XSLT 关键元素:“使用” “匹配”的父节点
我似乎无法弄清楚这件事。是否有可能拥有以下密钥?
<xsl:key name="kMatchSentenceID_withTokId" match="sentences/sentence/@ID"
use="--and here return the 'sentences' node--"/>
我不明白“use”是如何工作的,它不应该是匹配匹配时返回的值吗?
我看到 use="."
在我的例子中返回属性的值。 (为什么?它与匹配相关吗? .
不应该表示 node()
吗?而不是 node()/@
)重要的是,我为什么不能做这样的事情: use="parent::sentence[@ID=name()]"
那么
我该怎么做呢?我需要 @ID 上的匹配,但要返回它的父级(更具体地说,父级的 ID)。
谢谢。
I can't seem to figure this thing out. Is it at all possible to have the following key?
<xsl:key name="kMatchSentenceID_withTokId" match="sentences/sentence/@ID"
use="--and here return the 'sentences' node--"/>
I don't get how "use" works, isn't it supposed to be a value that you return when the match was matched?
I see that use="."
returns the value of the attribute in my case. (Why? Is it linked to the match? Shouldn't .
mean node()
? and not node()/@
)
But most importantly, how come I can't do something like this: use="parent::sentence[@ID=name()]"
How would I go about doing this then? I need a match on the @ID, but to return it's parent (more specifically, the parent's ID).
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,
use
没有指定任何要“返回”的内容。来自规范:在键到值的映射中,
use
指定键——以规范语言表示的键的值——而match
指定这些键将映射到的值。换句话说,match
指定要分组的内容,use
指定如何分组。假设您想通过父
sentence
的@ID
对sentence
元素进行分组。您可以这样做:您的表达式没有任何意义:
...因为它试图按其父
sentence
元素之一对ID
属性进行分组,该元素已经是错误,因为属性没有父级。No,
use
does not specify anything to be "returned". From the spec:In a mapping of keys to values
use
specifies the keys -- the values of the key, in the spec's language -- andmatch
specifies the values that those keys will map to. In other words,match
specifies what to group anduse
specifies how to group them .Say you wanted to group
sentence
elements by the@ID
of their parentsentence
. You could do that like this:Your expression makes no sense:
...because it's trying to group the
ID
attributes by one of their parentsentence
elements, which is already a mistake, since attributes don't have parents.假设这样的事情
你需要:
然后,例如 XPath
key('sentence','childa')/@id
将返回'父级'
。Assuming something like this
You need:
Then, for example the XPath
key('sentence','childa')/@id
would return'parent'
.帮助您有点困难,因为您实际上没有说出您要解决的问题是什么。但您似乎对键有一些概念上的混淆。可以这样想:如果您想查找其属性 P 的值为 V 的 E 元素,那么您需要将键定义为
其中 P 是一个 XPath 表达式,它使用 E 元素作为其上下文,确定相关属性的值;然后您需要将其称为key('N', V)
。 use 表达式可以是您喜欢的任何 XPath 表达式,并且将使用匹配的 E 元素作为其上下文项对其进行评估。你给出的例子似乎是合法的,但不是很有用;目前尚不清楚您希望他们做什么。
It's a bit difficult to help you since you don't actually say what problem you're trying to solve. But you seem to have some conceptual confusion about keys. Think of them this way: if you want to find the E elements that have the value V for their property P, then you need to define the key as
<xsl:key name="N" match="E" use="P"/>
where P is an XPath expression which, using an E element as its context, determines the value of the property in question; and then you need to call it askey('N', V)
. The use expression can be any XPath expression you like, and it will be evaluated using the matching E element as its context item.The examples you've given seem to be legal but not very useful; it's not clear what you were hoping they would do.