Lists Home |
Date Index |
> -----Original Message-----
> From: Roger L. Costello [mailto:email@example.com]
> Sent: Tuesday, August 26, 2003 9:46 AM
> To: firstname.lastname@example.org
> Subject: [xml-dev] XPath brain teaser
> These two XPath expressions are equivalent:
> (a) True
> (b) False
> Am I the only one who answered this: (a) True?
> This doesn't seem intuitive to me. Comments? /Roger
This is how XPath has always worked. From my article "Things To Know and
Avoid When Querying XML Documents with XPath" at
"Given the information above, we can go back to my original problem
query and see why it gave the unexpected results.
//*[position() = 1]/@on-loan
is actually an abbreviation for
/descendent-or-self::node()/child::*[position() = 1]/@on-loan
which selects every node in the document and retrieves the on-loan
attribute of the first child of each of the selected nodes. Judicious
use of parenthesis quickly fixes the problem and
(//*)[position() = 1]/@on-loan
which is short for
(/descendent-or-self::node()/child::*)[position() = 1]/@on-loan
is actually what I wanted.
Full Query: /descendent-or-self::node()/child::*[position()=1]
Query Results: Select the first child of every node in the document.
Full Query: (/descendent-or-self::node()/child::*)[position()=1]
Query Results: Select the first node in the document.
PITHY WORDS OF WISDOM
To succeed in politics, it is often necessary to rise above your
This posting is provided "AS IS" with no warranties, and confers no