W3C home > Mailing lists > Public > public-qt-comments@w3.org > July 2002

Does XPath 2.0 allow me to return a node and a subset of its child nodes?

From: Berend de Boer <berend@xsol.co.nz>
Date: Sun, 30 Jun 2002 18:09:49 -0400 (EDT)
Message-ID: <3D1F8197.7020701@xsol.co.nz>
To: public-qt-comments@w3.org




Hello All,

XPath 1.0 only seems to be able to return an element and ALL of its 
children. Does XPath 2.0 allow to write an expression that returns an 
element and SOME of its children?

Example:

         <publisher name="Addison Wesley">
                 <book title="... XML .."/>
                 <book title="... Eiffel ..."/>
                 <book title="... XML ..."/>
         </publisher>
         <publisher name="O'Reilly">
                 <book title="... XML ..."/>
                 <book title="... Java ..."/>
         </publisher>

I want to get all books where XML appears in the title, but I want to 
know the publisher as well. This is the result I want to see:

         <publisher name="Addison Wesley">
                 <book title="... XML .."/>
                 <book title="... XML ..."/>
         </publisher>
         <publisher name="O'Reilly">
                 <book title="... XML ..."/>
         </publisher>


I've been unable to express this query with XPath 1.0. 
publisher[book/@title='XML'] returns publishers and ALL books, and
publisher/book[@title='XML'] gives ONLY the books and does NOT have the 
parent <publisher> (read for '=' contains()...).


What I can understand from the specs, a for loop can do what I want. But 
can anyone suggest what it would look like? This is probably incorrect:

for $p in (//publisher)
return (<publisher name="$p">,
         for $b in //book[../@name=$p/@name]
         return $b,
         </publisher>)


But more importantly, is there an expression that doesn't use 
programming? My XPath expressions are meant for non-programmers, and a 
for loop is probably not easy to understand for them.

In the use cases (1.2.4.1 Q1) I found something like this:

<result>
   {
     let $b := document("publishers.xml")
     return
         filter($b//publisher | $b//publisher/book[@title='XML'])
   }
</result>


I really didn't understand this expression at all. I assume it's equal 
to [//publisher | //publisher/book[@title='XML'] but with XPath 1.0 this 
returns all publishers, including all their child elements, i.e. all 
books and not only the books with 'XML' in the title. Is XPath 2.0 
different in this respect? So does $b//publisher just return publishers 
and not the child nodes??

To give you some background: for a certain XML database I'm 
investigating what language is most appropiate to use to specify 
queries. XPath 1.0 seemed to be limited in its power, but a full 
programming language is too much. So I'm searching for a relative simple 
declarative query language, that isn't too hard to implement and can run 
fast on a relational database (the XML is stored in a relational 
database). So I like to know if XPath 2.0 is more suited to the task.


Any help appreciated.

Regards,

Berend. (-:
Received on Tuesday, 2 July 2002 11:39:41 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 16:56:43 UTC