XSL Grouping

Discussion in 'XML' started by boogz, Apr 28, 2010.

  1. boogz

    boogz

    Joined:
    Apr 28, 2010
    Messages:
    1
    Likes Received:
    0
    Hey there

    Having a bit of a problem, and totally unsure how to work around it.

    My XML file looks something like this

    <author>
    <name>Person A</name>
    <book>Book A</book>
    </author>
    <author>
    <name>Person A</name>
    <book>Book B</book>
    </author>

    <author>
    <name>Person B</name>
    <book>Book Z</book>
    </author>

    <author>
    <name>Person A</name>
    <book>Book Y</book>
    </book>

    I want to merge all books the authors have made into one

    ie.

    <author>
    <name>Person A</name>
    <book>Book A</book>
    <book>Book B</book>
    <book>Book Y</book>
    </book>

    <author>
    <name>Person B</name>
    <book>Book Z</book>
    </author>

    ----

    My Solution:

    I've attempted to try
    <xsl:template match="catalogue">
    <xsl:element name="author">
    <xsl:call-template name="name">
    <xsl:with-param name="pos" select="position()"/>
    </xsl:call-template>
    </xsl:element>
    </xsl:template>


    <xsl:template name="author">
    <xsl:param name="pos"/>
    <xsl:variable name="unique-names"
    select="//name[not(.=following::name)]" />
    <xsl:for-each select="$unique-names">
    <xsl:if test="$pos = position()">
    <xsl:element name="name"><xsl:value-of select="."/></xsl:element>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>

    -> Which gave me unique list of names

    <author>
    <name>Author A</name>
    </author>

    <author>
    <name>Author B</name>
    </author>

    Still unsure of how I would include the books to its corresponding name :S
    I'm new to XSLT so any help or suggestions would be of the greatest help!
     
    Last edited: Apr 28, 2010
    boogz, Apr 28, 2010
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.