Reputation: 21224
I have a system which evaluates XPath expressions (filters) relative to a node within the XML document. Normally a filter would match some sub node and check whether it exists. E.g., ./div[contains(@class, 'soldout')]
to filter out already sold out products.
I now want to express a filter expression based upon the current node. I tried the following:
.[contains(@class, 'soldout')]
But the system tells me that this is no valid XPath.
What's the correct way to express "current node" or "context node"?
Upvotes: 1
Views: 3407
Reputation: 163488
It's an unfortunate bug in the XPath 1.0 grammar that ".
" and "..
" can't be followed by a predicate (this was fixed in 2.0). It arises because of the rule
[4] Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep
(where an AbbreviatedStep
is .
or ..
)
So although it says in the spec that ".
" is equivalent to self::node()
, that's not strictly true, because self::node()
can be followed by a predicate, and .
can't.
Upvotes: 2
Reputation: 5915
You probably need self axis. It works like this :
path to the node/self::element to evaluate[predicate]
/path to the node/self::*[contains(@class, 'soldout')]
Upvotes: 1