XSLT 关键元素:“使用” “匹配”的父节点

发布于 2024-11-07 05:46:16 字数 510 浏览 1 评论 0原文

我似乎无法弄清楚这件事。是否有可能拥有以下密钥?

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉黛浅 2024-11-14 05:46:16

我不明白“使用”是如何运作的,不是吗?
应该是你返回的值
比赛何时匹配?

不,use 没有指定任何要“返回”的内容。来自规范

使用属性是
指定值的表达式
钥匙;表达式被求值
对于每个匹配的节点一次
模式。

在键到值的映射中,use 指定键——以规范语言表示的键的值——而 match 指定这些键将映射到的值。换句话说,match 指定要分组的内容use 指定如何分组。

假设您想通过父 sentence@IDsentence 元素进行分组。您可以这样做:

<xsl:key name="kMatchSentenceID_withTokId" match="sentences/sentence" 
         use="parent::*/@ID"/>

您的表达式没有任何意义:

<xsl:key name="kMatchSentenceID_withTokId" match="sentences/sentence/@ID"
         use="parent::sentence[@ID=name()]"/>

...因为它试图按其父 sentence 元素之一对 ID 属性进行分组,该元素已经是错误,因为属性没有父级。

I don't get how "use" works, isn't it
supposed to be a value that you return
when the match was matched?

No, use does not specify anything to be "returned". From the spec:

The use attribute is
an expression specifying the values of
the key; the expression is evaluated
once for each node that matches the
pattern.

In a mapping of keys to values use specifies the keys -- the values of the key, in the spec's language -- and match specifies the values that those keys will map to. In other words, match specifies what to group and use specifies how to group them .

Say you wanted to group sentence elements by the @ID of their parent sentence. You could do that like this:

<xsl:key name="kMatchSentenceID_withTokId" match="sentences/sentence" 
         use="parent::*/@ID"/>

Your expression makes no sense:

<xsl:key name="kMatchSentenceID_withTokId" match="sentences/sentence/@ID"
         use="parent::sentence[@ID=name()]"/>

...because it's trying to group the ID attributes by one of their parent sentence elements, which is already a mistake, since attributes don't have parents.

半夏半凉 2024-11-14 05:46:16

假设这样的事情

<sentences id="parent">
  <sentence id="childa"/>
  <sentence id="childb"/>
</sentences>

你需要:

<xsl:key name="sentence" match="sentences" use="sentence/@id"/>

然后,例如 XPathkey('sentence','childa')/@id 将返回
'父级'

Assuming something like this

<sentences id="parent">
  <sentence id="childa"/>
  <sentence id="childb"/>
</sentences>

You need:

<xsl:key name="sentence" match="sentences" use="sentence/@id"/>

Then, for example the XPathkey('sentence','childa')/@id would return
'parent'.

雪若未夕 2024-11-14 05:46:16

帮助您有点困难,因为您实际上没有说出您要解决的问题是什么。但您似乎对键有一些概念上的混淆。可以这样想:如果您想查找其属性 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 as key('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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文