473,609 Members | 2,187 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.ep s" size="4313" project="mars"/>
<file name="batboy.wk s" size="424" project="neptun e"/>
<file name="potrzebie .dbf" size="1102" project="jupite r"/>
<file name="kwatz.xom " size="43" project="jupite r"/>
<file name="paisley.d oc" size="988" project="neptun e"/>
<file name="ummagumma .zip" size="2441" project="mars"/>
<file name="schtroump f.txt" size="389" project="mars"/>
<file name="mondegree n.doc" size="1993" project="neptun e"/>
<file name="gadabout. pas" size="685" project="jupite r"/>
</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 5678
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
XslCompiledTran sform 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.ep s" size="4313" project="mars"/>
<file name="batboy.wk s" size="424" project="neptun e"/>
<file name="potrzebie .dbf" size="1102" project="jupite r"/>
<file name="kwatz.xom " size="43" project="jupite r"/>
<file name="paisley.d oc" size="988" project="neptun e"/>
<file name="ummagumma .zip" size="2441" project="mars"/>
<file name="schtroump f.txt" size="389" project="mars"/>
<file name="mondegree n.doc" size="1993" project="neptun e"/>
<file name="gadabout. pas" size="685" project="jupite r"/>
</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:styleshe et
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:styleshe et
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
2424
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 figure out how to do so. There is supposed to be a way of using templates and the key function to do grouping, but can it be done with 2 different trees? If so, are there any examples of such things Thanks for any help, Kevin
3
1806
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>. My XSL keeps returning zero for the count. The results for the XML/XSLT files below should look like:- Contact # of Visits Account ----------------------------------------------------------
5
1604
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> <unit sn="5"> <test-a> <result id="0">5</result> </test-a>
5
2161
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 it from a traditional programming point of view. I have did quite a bit of searching but have found no answers to my particular problem... please read below XML for the problem I am having.
2
2729
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 xmlns:bibxml="http://bibtexml.sf.net/"> <bibxml:entry id="foo01"> <bibxml:article> <bibxml:title>Foo</bibxml:title>
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 resulting node-set where these element values are the same. <SKU_INFO> <DETAIL_SEQ_NUMBER>11</DETAIL_SEQ_NUMBER> <SKU_TIE>1</SKU_TIE> <SKU_NUMBER>223344</SKU_NUMBER> <SKU_QUANTITY>2</SKU_QUANTITY> <MFG_PART_NUM>1234A</MFG_PART_NUM>...
2
1696
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 multiple nodes?" I will use an example to try to explain what I need to do and what I have for data. The example might not be very realistic but it's much easier than to try and explain using the scenario I have =P Suppose I had a list of...
0
1518
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 split a grouping into 3 parts? I've got the following grouping of countries which is working fine: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4
1949
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
8109
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8035
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8509
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8374
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5502
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4059
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2502
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 we have to send another system
1
1630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.