xsl-list
[Top] [All Lists]

Re: [xsl] XPath 1.0 Question: Can two sibling nodes each select their parent node to yield two nodes?

2010-09-22 17:51:34
Hi,

At 06:32 PM 9/22/2010, David wrote:
On 22/09/2010 22:08, Costello, Roger L. wrote:
Hi Folks,

This is an XPath 1.0 question.

Consider this document:

<?xml version="1.0" encoding="UTF-8"?>
<books>
     <category>
         <genre>Autobiography</genre>
         <book>
             <name>My Life and Times</name>
             <author>Paul McCartney</author>
         </book>
         <book>
             <name>The Beatles</name>
             <author>Paul McCartney</author>
         </book>
     </category>
     <category>
         <genre>Music</genre>
         <book>
             <name>Eight Days a Week</name>
             <author>Paul McCartney</author>
         </book>
     </category>
</books>

I would like to select the value of<genre> of each<book> with<author> equal to 'Paul McCartney'. Thus, I want the XPath to yield:

     Autobiography
     Autobiography
     Music

xpath1 either returns a single value (a string, boolean or number) or a node set. There is only one genre node with value Autobiography, so any xpath returning a set of nodes selected from the document can only return at most one node with value Autobiography.

Quite. And in fact, the values returned ('Autobiography', one time, 'Music', one time) do in fact satisfy the logic stated in English: "the value of <genre> of each <book> with <author> equal to 'Paul McCartney'". At least, that is, if we take "<genre>" to refer to the 'genre' element nodes in the document, and not a set of values mapped to the books (given that some of the 'book' elements share 'genre' elements).

So this can be a feature as often as a bug: de-duplication for free. (And when not, as Andrew says, use XSLT, XPath 2.0 or XQuery.)

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--