XSLT/XPath 中的当前节点与上下文节点? [英] Current node vs. Context node in XSLT/XPath?

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

问题描述

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

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.

您什么时候会使用其中一种?你是如何称呼每个人的?

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

推荐答案

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

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.

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

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.)

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

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.

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

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

<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>

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

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

foo\footnote{Batteries not included.}
bar

baz
aak\footnote{Some assembly required.}

eep
blech\footnotemark[2]

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

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 \footnote command; otherwise you want to write a \footnotemark command. You could use XSL code like this:

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

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

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

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

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.

这篇关于XSLT/XPath 中的当前节点与上下文节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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