[Home] [By Thread] [By Date] [Recent Entries]

  • To: "Roger L. Costello" <costello@m...>,<xml-dev@l...>
  • Subject: RE: XPath brain teaser
  • From: "Dare Obasanjo" <dareo@m...>
  • Date: Tue, 26 Aug 2003 09:58:07 -0700
  • Thread-index: AcNr8hq3PlvMU1zKSN2HDTywL1DqGAAAFzug
  • Thread-topic: 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. 

 

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member