473,509 Members | 6,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5661
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
2417
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
1801
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
1594
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
2152
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
2723
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
343
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
1688
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
1515
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
1944
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
7137
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
7347
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
7506
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...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.