Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: sorting and grouping

Reply
Thread Tools

XSLT: sorting and grouping

 
 
Christian Ludwig
Guest
Posts: n/a
 
      11-24-2003
Hello,

I have the following problem. Suppose you habe an XML File (containing
Bibliography-Data) of the form:

<bib>
<bibentry>
<title>Booktitle 1</title>
<year>1990</year>
<!-- some more elements -->
</bibentry>

<bibentry>
<title>Booktitle 2</title>
<year>1991</year>
<!-- more elements -->
</bibentry>

<!-- etc. -->

</bib>

Now, if I want to show all entries sorted by year, I do something like:

<xsl:for-each select="bib/bibentry">
<xsl:sort select="year" data-type="number" order="ascending" />
<p><xsl:value-of select="year"/>: <xsl:value-of select="title"/></p>
</xsl:for-each>

Now the problem:
How can I add (e.g.) extra space, when a new year starts: If I have 10
Books in the year 1990 and 5 books in the year 1991, I want to place a
special element in the output between the last book of 1990 and the
first book of 1991 in order to have a group per year.

To put it in a more abstract form:
How to implement decisions (conditionals) in the xsl-File, where the
condition depends not only on the current node, but also on the last node?


Can anybody help?

C. Ludwig

 
Reply With Quote
 
 
 
 
David Andriana
Guest
Posts: n/a
 
      11-26-2003
Christian Ludwig <> wrote:

> How can I add (e.g.) extra space, when a new year starts: If I have 10
> Books in the year 1990 and 5 books in the year 1991, I want to place a
> special element in the output between the last book of 1990 and the
> first book of 1991 in order to have a group per year.


<xsl:template match="/">

<!-- consider all years, one by one, sorted, no double -->
<xsl:for-each select="/bib/bibentry/year
[not(../preceding-sibling::bibentry/year = .)]">
<xsl:sort/>

<xsl:call-template name="handle-year">
<xsl:with-param name="year" select="."/>
</xsl:call-template>

</xsl:for-each>

</xsl:template>


<!-- display all bibentries for a give year -->
<xsl:template name="handle-year">
<xslaram name="year"/>

<xsl:variable name="bibentries" select="/bib/bibentry[year = $year]"/>

<H1>
Year <xsl:value-of select="$year"/> starts here,
it contains <xsl:value-of select="count($bibentries)"/> books.
</H1>

<xsl:for-each select="$bibentries">
<xsl:value-of select="title"/>
<br/>
</xsl:for-each>

<H1>
Year <xsl:value-of select="$year"/> ends here.
</H1>

</xsl:template>


--
David
 
Reply With Quote
 
 
 
 
Christian Ludwig
Guest
Posts: n/a
 
      11-26-2003
>
> <xsl:template match="/">
>
> <!-- consider all years, one by one, sorted, no double -->
> <xsl:for-each select="/bib/bibentry/year
> [not(../preceding-sibling::bibentry/year = .)]">
> <xsl:sort/>
>
> <xsl:call-template name="handle-year">
> <xsl:with-param name="year" select="."/>
> </xsl:call-template>
>
> </xsl:for-each>
>
> </xsl:template>
>


Thank you very much, works great.
C. Ludwig

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping/Sorting Data in Smart Markers, Better Conditional Formatting sherazam Java 0 10-21-2011 10:21 AM
Sorting, Grouping and Stacking Fruit2O DVD Video 10 02-08-2010 04:04 AM
Grouping/Sorting an Array of objects Howard Roberts Ruby 5 07-22-2008 02:56 PM
grouping/sorting [newbie question] bdarcus@gmail.com Ruby 6 06-27-2005 01:07 AM
Sorting and grouping with XSL Stylesheets Jack Wayne XML 0 01-24-2005 07:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57