473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XSLT: sorting and grouping

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

Jul 20 '05 #1
2 2051
Christian Ludwig <Lu******@gmx.de> 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">
<xsl:param 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
Jul 20 '05 #2
>
<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

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Graham | last post by:
Hi, I am having trouble getting XSL to count the members of a group. What I am trying to do is group by <objectid.Contactid> and count the number of <activityid>'s for each <objectid.contactid>....
5
by: inquirydog | last post by:
Hi- Does anyone know a way to compare whether two nodes contain the same information in xslt (the name, attributes, and all content recursivly should be the same. I am interested in the case...
5
by: Jody Greening | last post by:
Transforming with XSLT, Grouping elements until difference found. I am seeking some help with the following problem, I am fairly new at XSLT transformations, and my problem may lie in looking at...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
5
by: Mike | last post by:
Hello All, I have a report based upon a query. I have added a control to report footer that calcs the total cost of the inventory: =SUM(). When this total calculation is NOT on the report,...
2
by: Ryan Adler | last post by:
I have a terrible headache from this problem. Perhaps someone can help. My XML is formatted like this: <events> <event> <starts-at>123456<starts-at/> <event> ... <events/>
3
by: silver_sabrina | last post by:
Hey everyone, I'm having some trouble with this. I need to convert one xml doc into another and I think that XSLT may be the answer, so I'd like some help from the gurus out here :) If XSLT cannot...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
1
by: Sandeep Singh | last post by:
Hi, How to do group by in XSLT ? I tried on the following codes: <files> <file name="swablr.eps" size="4313" project="mars"/> <file name="batboy.wks" size="424" ...
4
by: kstevens | last post by:
I have a report, and on the report is a min, avg, and max query each with (ok who really cares) 20 or so records. At the footer of the report i would like to have a total. I requery for each...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.