XSLT:根据当前上下文读取其他节点的值 [英] XSLT: read value of other node based on current context

查看:20
本文介绍了XSLT:根据当前上下文读取其他节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人,

我有一个结构如下的 XML:

I have an XML with a structure like this:

<root>
  <concepts>
    <concept id="1" version="1">
      <name>This is the name of 1.1</name>
    </concept>
    <concept id="1" version="2">
      <name>This is the name of 1.2</name>
    </concept>
    <concept id="2" version="1">
      <name>This is the name of 2.1</name>
    </concept>
  </concepts>
  <structures>
    <structure id="1">
      <conceptRef id="2" version="1" />
    </structure>
    <structure id="2">
      <conceptRef id="1" version="2" />
    </structure>
  </structures>
</root>

我想根据结构/概念引用子节点中的属性值获取概念的名称子节点中的文本.上面例子的输出应该是这样的:

I want to get the text within the name child-node of concept based on attribute values in structure/conceptRef child node. The output for the example above should be along those lines:

  • 结构 1:这是 2.1 的名称
  • 结构 2:这是 1.2 的名称

所以我目前有这样的事情:

So I currently have something like this:

<xsl:template match="structures">
  <xsl:for-each select=".//structure">
    Structure <xsl:value-of select="@id" />: <!-- TODO: what goes here -->
  </xsl:for-each>
</xsl:template>

我不知道的是,我如何嵌套 XPath 查询以根据当前上下文从其他树中查找节点.出于调试目的,我现在添加了三个不同的行来测试该方法:

What I do not know is, how I can nest the XPath query to find the nodes from the other tree based on current context. For debugging purposes, I have now added three different lines to test the approach:

    <xsl:template match="structures">
        <xsl:for-each select=".//structure">
            Structure <xsl:value-of select="@id" />: 
             0: <xsl:value-of select="./conceptRef/@id" />.<xsl:value-of select="./conceptRef/@version" />
             a: <xsl:value-of select="//concepts/concept[@id=./conceptRef/@id and @version=./conceptRef/@version]/name" />
             b: <xsl:value-of select="//concepts/concept[@id=1 and @version=2]/name" />
        </xsl:for-each>
    </xsl:template>

输出为:

            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 行中合并时,结果由于某种原因为空.

It means that what is under 0 delivers the right values for the filter. In line 2 I see the hardcoded value that works as well. Just when I combine the two in line a, the result is for some reason empty.

有什么想法吗?

谢谢,丹尼尔.

推荐答案

我强烈建议使用 key 来解决交叉引用.以下样式表:

I strongly recommend using a key to resolve cross-references. The following stylesheet:

XSLT 1.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>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,将产生:

applied to your input example, will produce:

结果

Structure 1: This is the name of 2.1
Structure 2: This is the name of 1.2

这篇关于XSLT:根据当前上下文读取其他节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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