【问题标题】:When XSLT for-each is in a loop. How can I add an attribute or node to that XML based on other XML value. USING XSLT当 XSLT for-each 处于循环中时。如何根据其他 XML 值向该 XML 添加属性或节点。使用 XSLT
【发布时间】:2011-08-12 03:50:54
【问题描述】:

谁能帮我解决这个问题?

我有一个 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>

另一个 XML 存储在名为“data”的变量中(用于过滤):

<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>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    尝试使用带有键功能的查找表,如本例中的Tip: XSLT Lookup Table

    我能够获得以下 sn-p 来生成与您上面的输出相匹配的 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 对我非常有用。
    • 很高兴为您提供帮助。您应该通过单击它已回答您的问题的勾号来接受答案,如果您真的很高兴,甚至可以使用箭头“投票赞成”。
    • 这不会像问题那样过滤con/@id 上的a 元素,以可能的空order 元素结尾。在这种情况下,最好遍历比数据更稀有的过滤器(至多更短)。
    【解决方案2】:

    基于输入,现在我尝试并使用另一种表示形式实现。

    新的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2015-04-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多