XSL Grouping

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

  1. boogz

    boogz

    Joined:
    Apr 28, 2010
    Messages:
    1
    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank-Ralph Reiser

    Re: XSL: grouping data and group header?

    Frank-Ralph Reiser, Jul 31, 2003, in forum: XML
    Replies:
    0
    Views:
    1,453
    Frank-Ralph Reiser
    Jul 31, 2003
  2. kcwolle
    Replies:
    0
    Views:
    385
    kcwolle
    Nov 27, 2003
  3. Dimitre Novatchev

    .Re: Grouping neighboring elements with xsl

    Dimitre Novatchev, Nov 28, 2003, in forum: XML
    Replies:
    0
    Views:
    493
    Dimitre Novatchev
    Nov 28, 2003
  4. Jack Wayne
    Replies:
    0
    Views:
    469
    Jack Wayne
    Jan 24, 2005
  5. schaf@2wire.ch
    Replies:
    1
    Views:
    3,649
    A. Bolmarcich
    May 27, 2005
Loading...

Share This Page