[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: XPath brain teaser
> -----Original Message----- > From: Roger L. Costello [mailto:costello@m...] > Sent: Tuesday, August 26, 2003 9:46 AM > To: xml-dev@l... > Subject: 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.
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|