当 XSLT for-each 处于循环中时。如何根据其他 XML 值向该 XML 添加属性或节点。使用 XSLT
任何人都可以帮我解决这个问题吗?
我有一个 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>
Can any one help me to sort this problem?
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..
Input 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>
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>
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用带有 key 函数的查找表,如本例所示 提示:XSLT 查找表
我能够获取以下代码片段来生成与上面的输出相匹配的 xml 文档。下面的 xslt 中的过滤数据是从单独的文档加载的,但应该很容易适应。
Try using a lookup table with the key function as in this example Tip: XSLT Lookup Table
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.
根据输入,现在我尝试了&用另一种表示形式来实现。
新的 XSLT 代码:
Based on inputs, now I tried & implemented with another form of representation.
New XSLT Code: