[
Lists Home |
Date Index |
Thread Index
]
> -----Original Message-----
> From: Roger L. Costello [mailto:costello@mitre.org]
> Sent: Tuesday, August 26, 2003 9:46 AM
> To: xml-dev@lists.xml.org
> Subject: [xml-dev] XPath brain teaser
>
> These two XPath expressions are equivalent:
>
> //para[1]
> /descendant::para[1]
>
> (a) True
> (b) False
>
<snip />
> 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
http://msdn.microsoft.com/library/en-us/dnexxml/html/xml06172002.asp
"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.
ANOTHER EXAMPLE
Abbreviation: //*[1]
Full Query: /descendent-or-self::node()/child::*[position()=1]
Query Results: Select the first child of every node in the document.
Abbreviation: (//*)[1]
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
principles.
This posting is provided "AS IS" with no warranties, and confers no
rights.
|