The syntax .[predicate]
is not allowed in XPath 1.0 on account of the syntax rules (see the end of this post for the gritty details).
100% of the advice I have found says that the only option is to use self::node()
for this:
self::node()[Featured = 'true']
This XPath tester is even specifically designed to tell users to use self::node()[predicate]
if they try to use .[predicate]
, but this is not the only option.
A valid and more concise option is to just wrap the abbreviated step in parentheses:
(.)[Featured = 'true']
This is perfectly valid by XPath 1.0 syntax rules (and in my opinion, a lot clearer).
You can also use this approach with the ..
abbreviated step, even going up multiple levels:
Select grandfather node if it is featured
../..[Featured = 'true'] - Not valid
../../../*[Featured = 'true'] - Valid, but not accurate
../../self::node()[Featured = 'true'] - Valid, but verbose
(../..)[Featured = 'true'] - Valid
Addendum: Why it's not possible to use
.[predicate]
in XPath 1.0
The following is the definition of a "step" in XPath 1.0 (basically, the pieces of an XPath node selection expression separated by slashes are called "steps"):
[4] Step ::= AxisSpecifier NodeTest Predicate* | AbbreviatedStep
This means that one step consists of one of two possible options:
- An axis specifier (which can be an empty string), followed by a node test, followed by 0 or more predicates
- An abbreviated step:
.
or ..
There is no option to have an abbreviated step followed by predicates.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…