【问题标题】:Compare two XML files and get same value in XSLT比较两个 XML 文件并在 XSLT 中获得相同的值
【发布时间】:2021-04-07 10:41:14
【问题描述】:

我有两个 XML 文件。

资源.xml

<all>
    <response>
        <identifier>1234</identifier>
    </response>
    <response>
        <identifier>5678</identifier>
    </response>
</all>

和collection.xml

<collection>
    <response>
        <datas>1234</datas>
        <datas>202122</datas>
        <identifier>10</identifier>
    </response>
    <response>
        <datas>1234</datas>
        <datas>5678</datas>
        <datas>303132</datas>
        <identifier>11</identifier>
    </response>
</collection>

我想通过仅列出两个文件中存在的值将它们合并到一个 XML 文件中,如下例所示。 'line' 元素对应于 Collection 'response'。

<root>
    <line>
        <id>10</id>
        <refid>1234</refid>
    </line>
    <line>
        <id>11</id>
        <refid>1234</refid>
        <refid>5678</refid>
    </line>
</root>

对于以下 XSL 文件,缺少 refid。循环中的条件不起作用。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:variable name="ressource" select="document('../Input/ressource.xml')"/>
    <xsl:template match="collection">
        <root>
        <xsl:for-each select="response">
            <line>
                <xsl:variable name="datas" select="datas"/>
                <id>
                    <xsl:value-of select="identifier"/>
                </id>
                <xsl:for-each select="$ressource/response/identifier[. = $datas]">
                    <refid>
                        <xsl:value-of select="$datas"/>
                    </refid>
                </xsl:for-each>
            </line>
        </xsl:for-each>
        </root>
    </xsl:template>
</xsl:stylesheet>

提前谢谢你

【问题讨论】:

    标签: loops xslt merge


    【解决方案1】:

    第二个文档的路径应该以/all/response开头,而不是/response

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2023-03-03
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多