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

Help - use XSLT to group by multiple values?

I'm using XSLT and .NET Framework 1.1 to try to transform XML to group
by multiple values, and I'm not succeeding. I have source XML that
looks like the following:

<Data>
<Flavor>
<Name>Vanilla</Name>
<StartDate>20070704</StartDate>
<EndDate>20071231</EndDate>
</Flavor>
<Flavor>
<Name>Chocolate</Name>
<StartDate>20070704</StartDate>
<EndDate>20071131</EndDate>
</Flavor>
<Flavor>
<Name>Strawberry</Name>
<StartDate>20070804</StartDate>
<EndDate>20071131</EndDate>
</Flavor>
<Flavor>
<Name>Mint</Name>
<StartDate>20070704</StartDate>
<EndDate>20071131</EndDate>
</Flavor>
</Data>
I need to group the items together that have the same values for
StartDate and EndDate such that the output looks like the following:

<Groups>
<Group>
<Name>Group1</Name>
<Flavors>
<Name>Vanilla</Name>
</Flavors>
<StartDate>20070704</StartDate>
<EndDate>20071231</EndDate>
</Group>
<Group>
<Name>Group2</Name>
<Flavors>
<Name>Chocolate</Name>
<Name>Mint</Name>
</Flavors>
<StartDate>20070704</StartDate>
<EndDate>20071131</EndDate>
</Group>
<Group>
<Name>Group3</Name>
<Flavors>
<Name>Strawberry</Name>
</Flavors>
<StartDate>20070804</StartDate>
<EndDate>20071231</EndDate>
</Group2>
</Groups>

I've tried to use the Muenchian Method with keys, but I haven't been
successful. Can anyone please provide some assistance?

Thanks in advance

Jul 23 '07 #1
1 13345
n.phelge wrote:
I've tried to use the Muenchian Method with keys, but I haven't been
successful. Can anyone please provide some assistance?
Not sure where you had problems as you have not shown your XSLT but here
is a working XSLT 1.0 stylesheet:

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

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:key name="by-date" match="Flavor" use="concat(StartDate, '_',
EndDate)"/>

<xsl:template match="Data">
<Groups>
<xsl:apply-templates select="Flavor[generate-id() =
generate-id(key('by-date', concat(StartDate, '_', EndDate))[1])]"
mode="group"/>
</Groups>
</xsl:template>

<xsl:template match="Flavor" mode="group">
<Group>
<Name><xsl:value-of select="concat('Group', position())"/></Name>
<Flavors>
<xsl:apply-templates select="key('by-date', concat(StartDate,
'_', EndDate))/Name"/>
</Flavors>
<xsl:apply-templates select="StartDate | EndDate"/>
</Group>
</xsl:template>

<xsl:template match="Name | StartDate | EndDate">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jul 23 '07 #2

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

Similar topics

5
by: Chris Kettenbach | last post by:
Good Morning, Sorry for xposting. Just need a liitle help. I have an xml file that's generated from a database. How do I select distinct values from a field in xslt and then loop through the...
4
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
5
by: Patrick.O.Ige | last post by:
I have an xml and i'm trying to loop each node... When i do FOR EACH i seem not to get my desired result I want to loop through and get only the values that matches the question i specified with...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
2
by: Scott Zabolotzky | last post by:
I've got an XML file that contains both a data section and a custom schema section. The data may represent only a subset of the parameters defined by the schema. I'm new to XSLT and I'm trying...
1
by: n.phelge | last post by:
I'm using XSLT and .NET Framework 1.1 to try to transform XML to group by multiple values, and I'm not succeeding. I have source XML that looks like the following: <Data> <Flavor>...
4
by: sadc1986 | last post by:
How does one introduce multiple worksheets in a excel using Xslt Transforms my code... Please suggest <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
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" ...
2
Dormilich
by: Dormilich | last post by:
Hi, I'm testing my classes for a web page and I stumble upon an error I don't have a clue what it means: Error: Fatal error: Can't use method return value in write context in "output.php" on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.