In his recent blog-article: “XPath needs virtual axes. Making XPath more XPathy?” Rick Jelliffe sais:
“I really like XPath2. I would never recommend anyone start with XPath1, unless you were doing very basic transformations with no text processing or data formatting.
But the niggle I have with XPath2 is that it is less XPath-y than XPath1. It does not significantly improve the central syntactical feature of XPaths: the location steps. (The only improvement that springs to mind is that XPath2 did improve the use of parentheses in location steps.) Instead, XPath2 provided much more conventional features like a for iterator. I think these significantly decrease the comprehensibility of an XPath, are anonymous and therefore require may comments to explain them, and fracture the line. To an extent, once you start to use nested syntaxes and iterators, why both using XPath at all?”
Rick gives this example:
find-rep( find-client(//manager/clients/client-ref)/rep-ref)/name
and proposes to improve the expression above by introducing “virtual axes” so that it could be written as:
//manager/clients/client-ref/find-client::rep-ref/find-rep::rep/name
So, curious reader, pause for a while, don’t read below, and think: has Rick uncovered a hole in XPath?
What you are asking for can be expressed almost exactly in the same form (actually I prefer the current XPath 2.0 form of expression).
You are asking for:
//manager/clients/client-ref/find-client::rep-ref/find-rep::rep/name
One can write this in XPath 2.0 as:
//manager/clients/client-ref/find-client(.)/rep-ref/find-rep(.)/name
The current XPath way of expressing this is cleaner — no need for virtual axes.
This is a feature of XPath 2.0 that is not widely used and known: any function can be used as the current location step. The syntax rules that resolve its use are (at http://www.w3.org/TR/2007/REC-xpath20-20070123/#id-grammar):
[27] StepExpr ==> [38] FilterExpr ==> [41] PrimaryExpr
and the fact that a FunctionCall is a PrimaryExpr:
[41]
PrimaryExpr::=Literal | VarRef | ParenthesizedExpr | ContextItemExpr | FunctionCall
Also, consider that the axes in XPath have been useful so far mainly because there are just a few of them. Imagine having to deal with zillions of axes and struggling to remember what they mean. And if everyone can introduce their own axes, then why bother with them at all?
Dear reader, it’s up to you to decide… as I have already done.
Neat. I admit it. I’ve never used a function in an xpath.
Now there’s a good use of your blog (and your knowledge Dimitre), hunt out and explain the dark corners of XSLT 2.0?
DaveP
Dave,
Thank you for your high appreciation.
What do you mean by “dark corners of XSLT 2.0″?