当 XSLT for-each 处于循环中时.如何根据其他 XML 值向该 XML 添加属性或节点.使用 XSLT [英] 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

查看:19
本文介绍了当 XSLT for-each 处于循环中时.如何根据其他 XML 值向该 XML 添加属性或节点.使用 XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我解决这个问题?

Can any one help me to sort this problem?

我有一个 XML 并根据某些条件过滤值.将过滤后的 xml 存储在变量中.在过滤条件时,我试图向过滤后的 xml 添加一个属性或节点,但它对我不起作用..

I have an XML and filtering the values based on some condition. Storing the filtered xml in a variable. While filtering the condition, I'm trying to add an attribute or node to the filtered xml but it is not working for me..

输入 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(用于过滤):

Another XML which is stored in a variable called "data" (this is for filtering):

<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,我试图过滤 &以这种方式添加元素.

Using XSLT, I'm trying to filter & add a element in this way.

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

OUTPUT XML (i.e., "filteredData" variable should contain below 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>

推荐答案

尝试使用具有键函数的查找表,如本例所示 提示:XSLT 查找表

Try using a lookup table with the key function as in this example Tip: XSLT Lookup Table

我能够获得以下代码段来生成与上面的输出匹配的 xml 文档.下面的 xslt 中的过滤数据是从一个单独的文档中加载的,但应该很容易适应.

I was able to get the following snippet to produce an xml document which matched your output above. The filtering data in the xslt below has been loaded from a separate document but it should be easy to adapt.

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

这篇关于当 XSLT for-each 处于循环中时.如何根据其他 XML 值向该 XML 添加属性或节点.使用 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆