xml XSLT/XPath 中的当前节点与上下文节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1022345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:34:24  来源:igfitidea点击:

Current node vs. Context node in XSLT/XPath?

xmlxsltxpath

提问by Paul A Jungwirth

In XSLT, what is the difference between the "current node" and the "context node"? You can find both terms used here: http://www.w3.org/TR/xslt.

在 XSLT 中,“当前节点”和“上下文节点”有什么区别?您可以在此处找到使用的两个术语:http: //www.w3.org/TR/xslt

When would you use one or the other? How do you refer to each?

你什么时候会使用其中一个?你是如何引用每一个的?

回答by Paul A Jungwirth

The current node is whatever the template is currently operating on. Normally this happens to also be the context node, but the context node has special meaning within a nested XPath expression (the part in square brackets). There, it refers to whatever node is currently being tested for a match. Hence, the context node changes within the XPath expression, but not the current node.

当前节点是模板当前正在运行的任何节点。通常这恰好也是上下文节点,但上下文节点在嵌套的 XPath 表达式(方括号中的部分)中具有特殊含义。在那里,它指的是当前正在测试匹配的任何节点。因此,上下文节点在 XPath 表达式中发生了变化,而不是当前节点。

The context node can be abbreviated with a dot (.) or sometimes left out entirely. This is probably a little confusing, because outside of a nested expression, a dot signifies the current node. (In that case the current node happens to be the context node, so one might say that it is the current node only proximately, and it is more properly called the context node. But even the spec calls it the current node here.)

上下文节点可以用点 ( .)缩写或有时完全省略。这可能有点令人困惑,因为在嵌套表达式之外,点表示当前节点。(在这种情况下,当前节点恰好是上下文节点,所以人们可能会说它只是近似地是当前节点,更恰当地称为上下文节点。但即使是规范在这里也称其为当前节点。)

Since a dot gives you the context node, in a nested XPath expression the user needs a way to refer back to the current node, the one being processed by the current template. You can do this via the current()function.

由于点为您提供了上下文节点,因此在嵌套的 XPath 表达式中,用户需要一种方法来引用当前节点,即由当前模板处理的节点。您可以通过该current()功能执行此操作。

Distinguishing these two is useful in some cases. For instance, suppose you have some XML like this:

在某些情况下,区分这两者是有用的。例如,假设您有一些这样的 XML:

<a>
    <b>
        <c>foo<footnote fn="1"/></c>
        <d>bar</d>
    </b>
    <b>
        <c>baz</c>
        <d>aak<footnote fn="2"/></d>
    </b>
    <b>
        <c>eep</c>
        <d>blech<footnote fn="2"/></d>
    </b>
    <footnote-message fn="1">Batteries not included.</footnote>
    <footnote-message fn="2">Some assembly required.</footnote>
</a>

Now suppose you want to convert it to LaTeX like this:

现在假设您想像这样将其转换为 LaTeX:

foo\footnote{Batteries not included.}
bar

baz
aak\footnote{Some assembly required.}

eep
blech\footnotemark[2]

The trick is the tell whether a footnote has already been used or not. If this is the first time you've encountered the footnote, you want to write a \footnotecommand; otherwise you want to write a \footnotemarkcommand. You could use XSL code like this:

诀窍是判断脚注是否已被使用。如果这是你第一次遇到脚注,你想写一个\footnote命令;否则你想写一个\footnotemark命令。您可以像这样使用 XSL 代码:

<xsl:choose>
    <xsl:when test="count(preceding::*[./@fn = current()/@fn]) = 0">\footnote{...}</xsl:when>
    <xsl:otherwise>\footnotemark[...]</xsl:otherwise>
</xsl:choose>

Here we are comparing the context-node fnattribute (from the results of the preceding::*node-set) to the current-node fnattribute. (You don't actually have to say ./@fn; you could just say @fn.)

在这里,我们将上下文节点fn属性(来自preceding::*节点集的结果)与当前节点fn属性进行比较。(你实际上不必说./@fn;你可以说@fn。)

So in short, the context node leaves you inside the XPath predicate; the current node reaches outside the predicate, back to the node being processed by the current template.

简而言之,上下文节点将您留在 XPath 谓词中;当前节点到达谓词之外,回到当前模板正在处理的节点。

回答by kjhughes

ContextNode

上下文节点

The context nodeis part of the XPathevaluation contextand varies with each location step:

所述上下文节点是的一部分的XPath评估上下文以及与每个不同位置的步骤

step1 / step2 / step3 / ...

where each stepis

其中每个step

axis::node-test[predicate]
  • Each step is evaluated with respect to the context nodes set by the precedingsteps.
  • Each step then selects nodes that become the context node for followingsteps.
  • When evaluating predicate, the context node is the node along axisthat has passed node-test.
  • The context node can be accessed as ..
  • 每个步骤都根据前面步骤设置的上下文节点进行评估。
  • 每个步骤然后选择成为后续步骤的上下文节点的节点。
  • 在评估predicate中,上下文节点是沿路节点axis已经过去node-test
  • 上下文节点可以作为 访问.


CurrentNode

当前节点

The current node() is part of the XSLTprocessing model:1

所述当前节点()是一部分XSLT处理模型1

  • The current node is the node in the source XML document best matched by an XSLT template.
  • The current node becomes the starting context node for each XPath expression in the matched template.
  • The current node can be accessed as current()within XPath predicates.
  • 当前节点是源 XML 文档中与 XSLT 模板最匹配的节点。
  • 当前节点成为匹配模板中每个 XPath 表达式的起始上下文节点。
  • 可以像current()在 XPath 谓词中一样访问当前节点。


1Although insignificant to understanding the basic difference between context node and current node, note that in XSLT 2.0the description of the evaluation context has been changed. The concepts of current node and current node list have been replaced by the XPath concepts of context item, context position, and context size.

1尽管对于理解上下文节点和当前节点之间的基本区别无关紧要,但请注意,在 XSLT 2.0中,评估上下文的描述已更改。当前节点和当前节点列表的概念已被上下文项、上下文位置和上下文大小的 XPath 概念所取代。