[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: grouping data

Subject: Re: grouping data
From: "Vasu Chakkera" <vasucv@xxxxxxxxxxx>
Date: Fri, 05 Jul 2002 10:16:04 +0000
grouping data in xsl
I am sure you would have found the solution in the Archives..
The following code would produce the xml file you wanted..

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="book_on_name" match="/books/book" use="author/name"/>
<xsl:key name="book_on_type" match="/books/book" use="type"/>
<xsl:template match="/">
<!--grouping by author -->
<list>
<authors>
<xsl:for-each select="/books/book/author/name[not(.=preceding::name)]">
<author name = "{.}">
<xsl:for-each select="key('book_on_name',.)">
<isbn>
<xsl:value-of select="@isbn"/>
</isbn>
</xsl:for-each>
</author>
</xsl:for-each>
</authors>
<!--grouping by type -->
<booktype>
<xsl:for-each select="/books/book/type[not(.=preceding::type)]">
<type name = "{.}">
<xsl:for-each select="key('book_on_type',.)">
<book>
<isbn>
<xsl:value-of select = "@isbn"/>
</isbn>
<name>
<xsl:value-of select = "name"/>
</name>
</book>
</xsl:for-each>
</type>
</xsl:for-each>
</booktype>
</list>
</xsl:template>
</xsl:stylesheet>


Vasu


From: ªL ¤lªä <minikittygo@xxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  grouping data
Date: Fri, 05 Jul 2002 00:24:50 +0000


hi, i was wander anyone could help me on this topic


i have a structure like this:

<books>
<book isbn="1">
<author>
<name>author1</name>
<brithday>13/2/1965</brithday>
<description>...</description>
</author>
<type>adventure</type>
<name>bookName</name>
</book>
<book isbn="2">
<author>
<name>author2</name>
<brithday>13/2/1965</brithday>
<description>...something else </description>
</author>
<type>sci-fi</type>
<name>bookName</name>
</book>
<book isbn="3">
<author>
<name>author1</name>
<brithday>13/2/1965</brithday>
<description>...something else again </description>
</author>
<type>sci-fi</type>
<name>bookName</name>
</book>
<book isbn="4">
<author>
<name>author3</name>
<brithday>02/11/1975</brithday>
<description>...something else </description>
</author>
<type>novel</type>
<name>bookName</name>
</book>
...
</books>

i wish to group them in few way: first by the name of the author, then by
type, with the following result output:

<list>
<authors>
<author name="author1">
<isbn>1</isbn>    <!-- the attribute of its parents-->
<isbn>3</isbn>
</author>
<author name="author2">
<isbn>2</isbn>
</author>
...
</authors>
<booktype>
<type name="sci-fi">
<book>
<isbn>2</isbn>
<name>bookname</name>
</book>
<book>
<isbn>3</isbn>
<name>bookname</name>
</book>
...
</booktype>
...
</list>

i was i wondering how it could be achieve it.
i have try to set the following keys up:

xsl:key name="sort_author_name" match="author" use="name"/>
<xsl:key name="sort_booktype" match="book" use="type"/>

and sort them with the following template:
...
<xsl:template match="books">
<list>
<!-- for the sort by author name part -->
<authors>
<xsl:for-each
select="book/author[generate-id()=generate-id(key(sort_author_name,
name[1])]">
<unit><xsl:attribute name="name"><xsl:value-of
select="name"/></xsl:attribute>
<isbn><xsl:value-of select="../@isbn"/></isbn>
</xsl:for-each>
</authors>
...

</list>
</xsl:template>

however, i could not get anything out of it, can somebody comment on my
code, to see what went wrong?

Many thanks

regards
Kit

_________________________________________________________________
¨Ï¥Î¥þ²y³Ì¦h¤H¨Ï¥Îªº¹q¤l¶l¥óªA°È MSN Hotmail¡Ghttp://www.hotmail.com


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list






_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.