当XSLT for-each循环时,如何根据其他XML值向该XML添加属性或节点。使用XSLT。

5

有谁能帮我解决这个问题吗?

我有一个XML文件,根据某些条件过滤值。将过滤后的XML存储在变量中。在过滤条件时,我尝试向过滤后的XML添加属性或节点,但对我没有起作用。

输入的XML:

    <root>
        <a id="13">
            <b>XXX1</b>
            <c>YYY1</c>
        </a>
        <a id="2">
            <b>XXX2</b>
            <c>YYY2</c>
        </a>
        <a id="15">
            <b>XXX3</b>
            <c>YYY3</c>
        </a>
        <a id="37">
            <b>XXX4</b>
            <c>YYY4</c>
        </a>
        <a id="51">
            <b>XXX5</b>
            <c>YYY5</c>
        </a>
    </root>

另一个存储在名为"data"的变量中的XML(用于筛选):

<sample>
    <con id="37" order="1"/>
    <con id="13" order="2"/>
    <con id="51" order="3"/>
    <con id="2" order="4"/>
    <con id="15" order="5"/>
</sample>

使用XSLT,我正在尝试以以下方式过滤和添加元素。
<xsl:variable name="filteredData">
    <newroot>
      <xsl:for-each select="/root/a[@id > 14]">
        <xsl:if test="msxsl:node-set($data)/sample/con[@id = current()/@id]/@id = current()/@id">
          <xsl:element name="order">
            <xsl:value-of select="msxsl:node-set($data)/sample/con[@id = current()/@id]/@order"/>
          </xsl:element>
        </xsl:if>
      </xsl:for-each>
    </newroot>
</xsl:variable>

输出XML(即,“filteredData”变量应包含以下XML):

     <newroot>
        <a id="15">
            <b>XXX3</b>
            <c>YYY3</c>
            <order>5</order>
        </a>
        <a id="37">
            <b>XXX4</b>
            <c>YYY4</c>
            <order>1</order>
        </a>
        <a id="51">
            <b>XXX5</b>
            <c>YYY5</c>
            <order>3</order>
        </a>
    </newroot>
2个回答

1

尝试使用查找表,其中关键函数如此示例提示:XSLT查找表

我能够使以下片段生成与上面输出匹配的xml文档。下面的xslt中的过滤数据已从单独的文档加载,但应很容易适应。

<xsl:key name="id-lookup" match="con" use="@id"/>

<xsl:variable name="id-top" select="document('<lookup file>')/sample"/>

<xsl:template match="root">
    <newroot>
        <xsl:for-each select="a[@id > 14]">
            <xsl:copy>
                <xsl:copy-of select="@*|node()"/>

                <xsl:element name="order">
                    <xsl:apply-templates select="$id-top">
                        <xsl:with-param name="curr-label" select="."/>
                    </xsl:apply-templates>
                </xsl:element>
            </xsl:copy>
        </xsl:for-each>
    </newroot>
</xsl:template>


<xsl:template match="sample">
    <xsl:param name="curr-label"/>
    <xsl:value-of select="key('id-lookup', $curr-label/@id)/@order"/>
</xsl:template>

@stoney:太好了。感谢您的帮助,以及您建议的URL对我非常有用。 - Siva Charan
很高兴能够帮助你。如果我的答案解决了你的问题,你应该点击勾号接受它,甚至可以使用箭头进行“点赞”以表示满意。 - bstoney
这不会像问题中描述的那样过滤con / @id上的a元素,以可能为空的order元素结尾。在这种情况下,最好遍历过滤器(更短),而不是数据。 - user357812

1

根据输入,我现在尝试并实现了另一种表示形式。

新的XSLT代码:

<xsl:variable name="filteredData">        
    <newroot>          
      <xsl:for-each select="/root/a[@id > 14]">
        <xsl:copy>
          <xsl:copy-of select="@*|node()"/>
          <xsl:element name="Order">
            <xsl:choose>
              <xsl:when test="msxsl:node-set($data)/sample/con[@id = current()/@id]/@id = current()/@id">
                <xsl:value-of select="msxsl:node-set($data)/sample/con[@id = current()/@id]/@order"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>&#160;</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:element>
        </xsl:copy>
      </xsl:for-each>
    </newroot>
  </xsl:variable>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接