比较 XPath 中的属性值 [英] Comparing attribute values in XPath

查看:33
本文介绍了比较 XPath 中的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取特定节点的值,由其 id 属性指定.但是我的 XSL 解析器 Saxon 的行为并不是我预期的那样.

I want to get the value of a specific node, specified by its id attribute. But the behaviour of my XSL parser, Saxon, is not how I expected it to work.

这是我的 XSL 代码:

This is my XSL code:

<xsl:template match="synonyme">
    <xsl:element name="corr">
        <xsl:value-of select="@connecteur" />
        <xsl:value-of select="/liste/connecteur[@id=@connecteur]/forme[1]" />
    </xsl:element>
</xsl:template>

我刚刚匹配了一个名为 synonyme 的标签,它有一个 connecteur 属性.我的代码输出这个属性的值.

I just matched a tag named synonyme which has a connecteur attribute. My code outputs the value of this attribute.

我还想输出另一个节点的值,该节点的 id 属性与我当前匹配的 synonyme 标签的 connecteur 属性匹配.但是没有找到这个查询的结果,第二个 value-of 总是有空输出.

I also want to output the value of another node which has an id attribute matching the connecteur attribute of my currently matched synonyme tag. But no results are ever found for this query, the second value-of always has empty output.

如果我写,例如[@id='c160'],其中 c160 与第一个 value-of 输出的内容完全相同,它可以工作!但与我匹配节点的 @attribute 相比时则不然.我该如何解决这个问题?

If I write, e.g. [@id='c160'], where c160 is the exact same thing that is output by the first value-of, it works! But not when comparing to the @attribute of my matched node. How can I fix this?

XML 基本上是

<liste><connecteur id="c160"><forme>foo</forme></connecteur>
       <connecteur id="c161"><synonyme connecteur="c160" /></connecteur>
</liste>

并且代替 synonyme 的预期输出是 <corr>c160 foo</corr>.

and the expected output in place of the synonyme is <corr>c160 foo</corr>.

推荐答案

你使用的谓词:

[@id=@connecteur]

正在寻找具有两个属性的元素 - idconnecteur - 具有相同的值.要查找具有 id 属性且其值与当前元素的 connecteur 值匹配的元素,您需要使用:

is looking for an element with two attributes - id and connecteur- with equal values. To look for an element with an id attribute whose value matches the value of the current element's connecteur value, you need to use:

[@id=current()/@connecteur]

参见:https://www.w3.org/TR/xslt/#function-current

更好的解决方案是将定义为:

A better solution would be to define a key as:

<xsl:key name="ref" match="connecteur" use="@id" />

然后使用:

<xsl:value-of select="key('ref', @connecteur)/forme" />

解决交叉引用问题.

参见:https://www.w3.org/TR/xslt/#key

这篇关于比较 XPath 中的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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