轴的相反方向究竟何时适用? [英] Exactly when does the reverse direction of an axis apply?

查看:61
本文介绍了轴的相反方向究竟何时适用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熟悉 XPath 的人都知道有些轴,例如 preceding::,是反向轴.如果你在用反向轴构建的表达式上放置一个位置谓词,你可能会向后计数而不是向前计数.例如

Those who are familiar with XPath know that some axes, such as preceding::, are reverse axes. And if you put a positional predicate on an expression built with a reverse axis, you may be counting backward instead of forward. E.g.

$foo/preceding-sibling::*[1]

返回 $foo 之前的同级元素,而不是第一个之前的同级元素(按文档顺序).

returns the preceding sibling element just before $foo, not the first preceding sibling element (in document order).

但是随后您会遇到此规则似乎被打破的变化,这取决于位置谓词与反向轴的距离.例如

But then you encounter variations where this rule seems to be broken, depending on how far removed the positional predicate is from the reverse axis. E.g.

($foo/preceding-sibling::*)[1]

从文档开始向前计数,而不是从 $foo 向后计数.

counts forward from the beginning of the document, not backward from $foo.

今天我正在写一些代码,其中我有一个表达式

Today I was writing some code where I had an expression like

$foo/preceding::bar[not(parent::baz)][1]

我想从 $foo 开始倒数.但是我的位置谓词是否离 preceding:: 轴太远了?在我添加 [1] 之前,表达式是否失去了反向方向?我认为它可能不起作用,所以我将其更改为

I wanted to be counting backwards from $foo. But was my positional predicate too far removed from the preceding:: axis? Had the expression lost its reverse direction before I added the [1]? I thought it probably wouldn't work, so I changed it to

$foo/preceding::bar[not(parent::baz)][last()]

但后来我不太确定方向,所以我加了括号以确保:

but then I wasn't really sure of the direction, so I put in parentheses to make sure:

($foo/preceding::bar[not(parent::baz)])[last()]

然而,额外的括号有点令人困惑,我认为表达式可能效率较低,如果它真的必须从(大)输入文档的开头开始计算,而不是从 $foo.真的有必要这样做吗?

However, the extra parentheses are a bit confusing, and I thought the expression might be less efficient, if it really has to count from the beginning of the (large) input document instead of backward from $foo. Was it really necessary to do it this way?

最后我测试了原来的表达式,惊讶地发现它起作用了!所以中间的 [not(parent::baz)] 并没有导致表达式失去它的反向方向.

Finally I tested the original expression, and found to my surprise that it worked! So the intervening [not(parent::baz)] had not caused the expression to lose its reverse direction after all.

那个问题已经解决了,但我已经到了我想要更好地处理何时可以应用轴的反向的地步.我的问题是:使用反向轴的 XPath 表达式在什么时候会失去反向方向?

That problem was solved, but I've come to the point where I'd like to get a better handle on when I can expect the reverse direction of an axis to apply. My question is: At what point(s) does an XPath expression using a reverse axis lose its reverse direction?

我相信我现在已经找到了答案,所以我来回答我自己的问题.但我无法在 SO 上找到答案,这件事困扰了我很长时间,值得在这里提问和回答.

I believe I've found the answer now, so I'll answer my own question. But I couldn't find the answer on SO, and it's something that has bothered me long enough that it was worth asking and answering here.

推荐答案

我找到的最佳答案是在旧的 埃文·伦茨的电子邮件.

The best answer I found was in an old email by Evan Lenz.

值得一读,它解释了 XPath 在这方面的工作原理,以及 XPath 1.0 规范如何向我们展示答案.但执行摘要在此规则中:

It's worth reading in full, as an explanation of how XPath works in this regard, and how the XPath 1.0 spec shows us the answer. But the executive summary is in this rule:

Step    ::=    AxisSpecifier NodeTest Predicate*
                  | AbbreviatedStep

Step 产生式定义了位置步骤的语法,并且仅在一个位置步骤内,轴的反向应用.除了 nodetest 和 predicates 之外,任何出现在轴和位置谓词之间的语法都会打破链条,方向将恢复为向前.

The Step production defines the syntax of a location step, and it's only within a location step that the reverse direction of an axis applies. Any syntax that comes between the axis and a positional predicate, other than the nodetest and predicates, will break the chain and the direction will revert to forward.

这就解释了为什么,如果您将括号放在 preceding::foo 周围并在括号外附加位置谓词,位置谓词会忽略 preceding:: 的方向> 轴.

This explains why, if you put parentheses around a preceding::foo and append a positional predicate outside the parentheses, the positional predicate ignores the direction of the preceding:: axis.

这也解释了为什么我今天在我的代码中的第一次尝试成功了,尽管我的期望是:您可以根据需要在 NodeTest 之后放置任意数量的谓词,并且轴的方向仍然适用于所有谓词.

It also explains why my first attempt in my code today worked, despite my expectations: you can put as many predicates after a NodeTest as you want, and the direction of the axis will still apply to all of them.

这篇关于轴的相反方向究竟何时适用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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