[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: Re: [xsl] XPath 1.0 Question: Can two sibling nodes each select their parent node to yield two nodes?
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Wed, 22 Sep 2010 22:45:16 +0100

On 22 September 2010 22:08, Costello, Roger L. <costello@xxxxxxxxx> 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
>
> I thought that either of these XPath expressions would do the job:
>
>    //book[author='Paul McCartney']/../genre
>
>    //book[author='Paul McCartney']/preceding-sibling::genre
>
> However, both result in this:
>
>    Autobiography
>    Music
>
> What XPath 1.0 expression will produce the desired result?

I don't think you can do it in XPath 1.0, you either need XPath 2.0 or
XSLT to iterate or avoid the de-duplication.


--
Andrew Welch
http://andrewjwelch.com


Current Thread
Keywords