[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XPath brain teaser

  • 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

abbreviation brain teasers


> -----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!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.