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: "Ludwig, Michael" <Michael.Ludwig@xxxxxxxxxxxx>
Date: Thu, 23 Sep 2010 09:28:07 +0200
> <?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

This is not your stated requirement, but maybe the real one:

<categories>
    <category><genre count="2">Autobiography</genre></category>
    <category><genre count="1">Music</genre></category>
</categories>

Produced like this:

<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

        <xsl:template match="books">
                <categories>
                        <xsl:apply-templates/>
                </categories>
        </xsl:template>

        <xsl:template match="books/category">
                <xsl:copy>
                        <xsl:apply-templates select="genre"/>
                </xsl:copy>
        </xsl:template>

        <xsl:template match="books/category/genre">
                <genre count="{ count(../book) }">
                        <xsl:apply-templates/>
                </genre>
        </xsl:template>

</xsl:stylesheet>

But I'd probably rethink your input data structure.

--
Michael Ludwig

Current Thread