XSLT:根据当前上下文读取其他节点的值
人, 我有一个结构如下的XML:XSLT:根据当前上下文读取其他节点的值,xslt,xpath,Xslt,Xpath,人, 我有一个结构如下的XML: 这是1.1的名称 这是1.2的名称 这是2.1的名称 我想根据structure/conceptRef子节点中的属性值获取概念的name子节点中的文本。上述示例的输出应符合以下要求: 结构1:这是2.1的名称 结构2:这是1.2的名称 所以我现在有这样的东西: 结构: 我不知道的是,如何嵌套XPath查询,以便根据当前上下文从另一棵树中查找节点。出于调试目的,我现在添加了三行代码来测试该方法: 结构: 0: . a: b: 输出为:
这是1.1的名称
这是1.2的名称
这是2.1的名称
我想根据structure/conceptRef子节点中的属性值获取概念的name子节点中的文本。上述示例的输出应符合以下要求:
- 结构1:这是2.1的名称
- 结构2:这是1.2的名称
结构:
我不知道的是,如何嵌套XPath查询,以便根据当前上下文从另一棵树中查找节点。出于调试目的,我现在添加了三行代码来测试该方法:
结构:
0: .
a:
b:
输出为:
Structure 1:
0: 2.1
a:
b: This is the name of 1.2
Structure 2:
0: 1.2
a:
b: This is the name of 1.2
这意味着0下的值为过滤器提供了正确的值。在第2行中,我看到了同样有效的硬编码值。就在我将这两个元素合并到a行时,由于某种原因,结果是空的
有什么想法吗
谢谢,
Daniel。您想使用
current()
函数,例如//concepts/concept[@id=current()/conceptRef/@id和@version=current()/conceptRef/@version]/name
作为工作路径。为了提高效率,您可能希望通过声明一个键并使用key函数来替换该查找。我强烈建议使用来解决交叉引用。以下样式表:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:key name="concept" match="concept" use="concat(@id, '|', @version)" />
<xsl:template match="/root">
<xsl:for-each select="structures/structure">
<xsl:text>Structure </xsl:text>
<xsl:value-of select="@id" />
<xsl:text>: </xsl:text>
<xsl:value-of select="key('concept', concat(conceptRef/@id, '|', conceptRef/@version))/name" />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
谢谢!我不知道关键功能。到目前为止,我把很多事情弄得太复杂了:)
Structure 1: This is the name of 2.1
Structure 2: This is the name of 1.2