473,320 Members | 1,969 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,320 software developers and data experts.

Grouping in XSLT

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" project="neptune"/>
<file name="potrzebie.dbf" size="1102" project="jupiter"/>
<file name="kwatz.xom" size="43" project="jupiter"/>
<file name="paisley.doc" size="988" project="neptune"/>
<file name="ummagumma.zip" size="2441" project="mars"/>
<file name="schtroumpf.txt" size="389" project="mars"/>
<file name="mondegreen.doc" size="1993" project="neptune"/>
<file name="gadabout.pas" size="685" project="jupiter"/>
</files>
<xsl:template match="files">
<xsl:for-each-group select="file" group-by="@project">
<!-some code-->
</xsl:for-each-group>
</xsl:template>

but it gives a sentax error

'xsl:for-each-group ' can not be a child of 'files' element.

Can any one help me out ?

Thanks in advance.

Sandy.

Please reply on the same group.
Jun 27 '08 #1
1 5639
Sandeep Singh wrote:
How to do group by in XSLT ?
With XSLT 2.0 you use xsl:for-each-group and current-group() and
current-grouping-key(), with XSLT 1.0 you use Muenchian grouping.
XSLT 2.0 is currently supported by Saxon 9 from
http://saxon.sourceforge.net/ which has Java and .NET versions and
interfaces, by AltovaXML tools http://www.altova.com/altovaxml.html and
by Gestalt http://gestalt.sourceforge.net/.

With XSLT 1.0 as supported by MSXML 3, 4, 5, 6 and XslTransform and
XslCompiledTransform and Firefox and Opera 9 and Safari (and lots of
others) you use Muenchian grouping, see
http://www.jenitennison.com/xslt/grouping/index.xml
I tried on the following codes:

<files>
<file name="swablr.eps" size="4313" project="mars"/>
<file name="batboy.wks" size="424" project="neptune"/>
<file name="potrzebie.dbf" size="1102" project="jupiter"/>
<file name="kwatz.xom" size="43" project="jupiter"/>
<file name="paisley.doc" size="988" project="neptune"/>
<file name="ummagumma.zip" size="2441" project="mars"/>
<file name="schtroumpf.txt" size="389" project="mars"/>
<file name="mondegreen.doc" size="1993" project="neptune"/>
<file name="gadabout.pas" size="685" project="jupiter"/>
</files>
<xsl:template match="files">
<xsl:for-each-group select="file" group-by="@project">
<!-some code-->
</xsl:for-each-group>
</xsl:template>

but it gives a sentax error

'xsl:for-each-group ' can not be a child of 'files' element.
Which XSLT processor do you use? See above for a list of processors
which support xsl:for-each-group.

Here is an example using Muenchian grouping with XSLT 1.0:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:key name="by-project" match="file" use="@project"/>

<xsl:output method="text"/>

<xsl:template match="files">
<xsl:apply-templates select="file[generate-id() =
generate-id(key('by-project', @project)[1])]"/>
</xsl:template>

<xsl:template match="file">
<xsl:value-of select="concat(@project, ': ')"/>
<xsl:for-each select="key('by-project', @project)">
<xsl:value-of select="@name"/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>
<xsl:value-of select="' '"/>
</xsl:template>

</xsl:stylesheet>
Text output is:

mars: swablr.eps, ummagumma.zip, schtroumpf.txt
neptune: batboy.wks, paisley.doc, mondegreen.doc
jupiter: potrzebie.dbf, kwatz.xom, gadabout.pas

The same result achieved can be achieved with XSLT 2.0:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="text"/>

<xsl:template match="files">
<xsl:for-each-group select="file" group-by="@project">
<xsl:value-of select="concat(@project, ': ')"/>
<xsl:value-of select="current-group()/@name" separator=", "/>
<xsl:value-of select="' '"/>
</xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2

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

Similar topics

3
by: Kevin Brown | last post by:
Is there anyway to generate this type of resulting HTML table from this XML using XSLT? Basically I need to be able to consult 2 trees of data to generate the HTML, but I have not been able to...
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: Mike King | last post by:
I don't know how to group the following data in the way I want it. I want the output of the transformation to be "5678". Does anyone know what I am doing worry? <?xml version="1.0"?> <data>...
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...
2
by: Tristan Miller | last post by:
Greetings. I have a question involving sorting and grouping output using XSLT. I am trying to produce a publication list from a BibTeX-like XML file: <bibxml:file...
0
by: MEC6304 | last post by:
I have the following fragment repeated mulitple times and need to sum the SKU_QUANTITY and transform the XML based upon a grouping of the SKU_TIE, SKU_NUMBER, and MFG_PART_NUM, ie display the...
2
by: Andreas Håkansson | last post by:
Seeing how my previous post seem to have fallen between the cracks, I thought I would have a second, more direct, go at it. So my question is "Is it possible to group (Muenchian method) over...
0
by: Hvid Hat | last post by:
Hi At first, I thought I could only solve my problem with a C# method inside my XSLT but I'm beginning to think it might be possible with XSLT only. So I'm trying, but I need help :-) How can I...
4
by: MRe | last post by:
Hi, Is it possible using XSLT to transform this.. <test> <b>0</b> <a>1</a> <a>2</a> <b>3</b> <b>4</b>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.