本文介绍了xslt-当该内容由2个文件共享时,用引用替换内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出几个我称为source1.xml的XML文件,例如:(节点可能会有所不同):

Given several XML files that I will call source1.xml that look like this for example (nodes may vary):

<root id="s1">
    <title>example</title>
        <p>Some body text.</p>
        <div>
            <ul>
                <li>Some list item</li>
                <li>Some list item</li>
            </ul>
            <p>qqwerty</p>
            <p>asdfg</p>
        </div>
</root>

参考文件

还有我将称为reference1.xml的其他文件,如下所示:

Reference files

And other files that I'll call reference1.xml that look like this:

<root>    
    <div>
        <ul>
            <li>Some list item</li>
            <li>Some list item</li>
        </ul>
        <p>qqwerty</p>
        <p>asdfg</p>
     </div>
<root>

reference2.xml看起来像这样:

<root>    
    <div>
       <p>Some body text.<p>
    </div>
</root>    

每个文件中只能有一个div元素.

There can only be one div element in each of these files.

要将它们映射在一起,我有一个看起来像这样的文件:

To map these together I have a file that looks like this:

<references>  
   <input src="source1.xml" id="s1">
      <reference>T:\temp\reference1.xml</reference>
      <reference>T:\temp\reference2.xml</reference>
   </input>
</references>

预期结果

我想获得以下信息:

Expected result

I would like to get the following:

<root id="s1">
    <title>example</title>
        <link href="reference2.xml"/>
        <div>
            <link href="reference1.xml"/>
        </div>
</root>

因此,我的想法是我想看到reference.xmldiv节点的全部内容可以在source.xml中的AS/IS中找到,如果是这样,只需在源代码中插入对其的引用即可.它必须是一个哑替换",如果找到它,无论源内容在哪里,无论父元素或其他内容如何,​​我都会在源中替换它.

So the idea is that I want to see the whole content of the div node of reference.xml can be found AS/IS in source.xml and if so, just insert a reference to it in the source. It must a "dumb replace", if I find it, I replace it in the source, wherever the content might be, no matter of the parent element or whatever.

如果在源中仅找到某些元素,则这是不可行的,因此不会创建任何链接.创建链接必须完全相同.

If only certain elements are found in the source, this is a no go, no link is created. It must be the exact same thing for a link to be created.

如何使用XSLT 2.0做到这一点?

How do I do this with XSLT 2.0?

现在,使用答案中建议的代码,我得到以下输出:

Right now, with the code suggested in the answer I get the following output:

<root id="s1">
    <title>example</title>
        <link href="reference2.xml"/>
        <div>
            <link href="reference1.xml"/>
            <link href="reference1.xml"/>
            <link href="reference1.xml"/>
        </div>
</root>

而不是:

<root id="s1">
    <title>example</title>
        <link href="reference2.xml"/>
        <div>
            <link href="reference1.xml"/>
        </div>
</root>

推荐答案

如果只有一个要匹配的元素,则可以这样做

If it is only a single element to be matched against then doing

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:param name="ref-doc-uri" select="'reference.xml'"/>
    <xsl:variable name="ref-doc" select="doc($ref-doc-uri)"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* , node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[deep-equal(., $ref-doc//ComponentDefinition/*)]">
        <link href="{$ref-doc-uri}"/>
    </xsl:template>

</xsl:stylesheet>

就足够了.

根据您对要求的修改,我将样式表增强为

Based on your edit of the requirements I have enhanced the stylesheet as

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:variable name="main-doc-uri" select="document-uri()"/>

    <xsl:param name="ref-list-doc-uri" select="'reference-list.xml'"/>
    <xsl:variable name="ref-list-doc" select="doc($ref-list-doc-uri)"/>

    <xsl:variable name="ref-docs" select="document($ref-list-doc/references/input[resolve-uri(@src) = $main-doc-uri]/reference)"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* , node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:variable name="matched-doc" select="$ref-docs[ReusableComponent/ComponentDefinition/*[deep-equal(., current())]]"/>
        <xsl:choose>
            <xsl:when test="$matched-doc">
                <link href="{tokenize(document-uri($matched-doc), '/')[last()]}"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:next-match/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

我假设reference-list.xml具有类似

<references>
    <input src="source1.xml">
        <reference>ref1.xml</reference>
        <reference>ref2.xml</reference>
    </input>
</references>

但是,我还没有尝试使用带有多个源文件和参考文件的代码,因此您需要进行自我测试.

I haven't tried that code with several source and reference files, however, so you need to test yourself.

如果参考文档中可以使用不同的名称空间,那么也许使用通配符会有所帮助:

If there can be different namespaces used in the reference document then perhaps using a wild card helps:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:variable name="main-doc-uri" select="document-uri()"/>

    <xsl:param name="ref-list-doc-uri" select="'reference-list.xml'"/>
    <xsl:variable name="ref-list-doc" select="doc($ref-list-doc-uri)"/>

    <xsl:variable name="ref-docs" select="document($ref-list-doc/references/input[resolve-uri(@src) = $main-doc-uri]/reference)"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* , node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:variable name="matched-doc" select="$ref-docs[*:ReusableComponent/*:ComponentDefinition/*[deep-equal(., current())]]"/>
        <xsl:choose>
            <xsl:when test="$matched-doc">
                <link href="{tokenize(document-uri($matched-doc), '/')[last()]}"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:next-match/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

那样,对于输入

<?xml version="1.0" encoding="UTF-8"?>
<root id="s1">
    <title>example</title>
    <p>Some body text.</p>
    <div>
        <ul>
            <li>Some list item</li>
            <li>Some list item</li>
        </ul>
    </div>
</root>

我得到了输出

<?xml version="1.0" encoding="UTF-8"?>
<root id="s1">
   <title>example</title>
   <link href="reference201605270302.xml"/>
   <div>
      <link href="reference201605270301.xml"/>
   </div>
</root>

要基于resolve-uri(@src) = $main-doc-uri中URI的比较来选择要加载的文档,如果您想基于id属性来执行此操作,那么我想您希望<xsl:variable name="ref-docs" select="document($ref-list-doc/references/input[@id = $main-doc/*/@id]/reference)"/>具有全局变量<xsl:variable name="main-doc" select="/"/>

The documents to be loaded are selected based on a comparison of the URIs in resolve-uri(@src) = $main-doc-uri, if you want to do that based on the id attribute then I guess you want <xsl:variable name="ref-docs" select="document($ref-list-doc/references/input[@id = $main-doc/*/@id]/reference)"/> with a global variable <xsl:variable name="main-doc" select="/"/>.

这篇关于xslt-当该内容由2个文件共享时,用引用替换内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:43