XSLT:如何通过另一个节点查找节点的值 [英] XSLT: How to find value of the node by another node
问题描述
我不确定我问的问题是否正确,这就是为什么我在任何地方都找不到答案.但基本上我需要将一个节点与另一个节点匹配并使用兄弟节点作为值.这是一个例子
I am not sure I am asking the question correctly, that's why I cannot find an answer anywhere. But basically I need to match a node with another node and use a sibling node as a value instead. Here is an example
<group>
<section>
<reference>123</reference>
<name>ABC</name>
</section>
<section>
<reference>456</reference>
<name>DEF</name>
</section>
</group>
<element>
<reference>123</reference>
<price>20.00</price>
</element>
在我的 XSL 模板中,我想显示价格和名称,因此我需要将来自元素的参考与部分中的参考相匹配并显示名称.
And in my XSL template I want to display Price and Name, so I need to match Reference from the Element to Reference in Section and display Name.
ABC - 20.00
ABC - 20.00
我该怎么做?
推荐答案
我需要将元素中的引用与节中的引用相匹配,并且显示名称.
I need to match Reference from the Element to Reference in Section and display Name.
XSLT 有一个名为 key 的特殊功能只是为了这个目的.例如,以下样式表:
XSLT has a special feature called key just for this purpose. For example, the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="section" match="section" use="reference" />
<xsl:template match="/root">
<output>
<xsl:for-each select="element">
<item>
<xsl:value-of select="key('section', reference)/name"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="price"/>
</item>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
应用于以下格式良好的输入时:
XML
<root>
<group>
<section>
<reference>123</reference>
<name>ABC</name>
</section>
<section>
<reference>456</reference>
<name>DEF</name>
</section>
</group>
<element>
<reference>123</reference>
<price>20.00</price>
</element>
</root>
将返回:
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>
<item>ABC - 20.00</item>
</output>
这篇关于XSLT:如何通过另一个节点查找节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!