473,883 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>2007 0704</StartDate>
<EndDate>200712 31</EndDate>
</Flavor>
<Flavor>
<Name>Chocolate </Name>
<StartDate>2007 0704</StartDate>
<EndDate>200711 31</EndDate>
</Flavor>
<Flavor>
<Name>Strawberr y</Name>
<StartDate>2007 0804</StartDate>
<EndDate>200711 31</EndDate>
</Flavor>
<Flavor>
<Name>Mint</Name>
<StartDate>2007 0704</StartDate>
<EndDate>200711 31</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>2007 0704</StartDate>
<EndDate>200712 31</EndDate>
</Group>
<Group>
<Name>Group2</Name>
<Flavors>
<Name>Chocolate </Name>
<Name>Mint</Name>
</Flavors>
<StartDate>2007 0704</StartDate>
<EndDate>200711 31</EndDate>
</Group>
<Group>
<Name>Group3</Name>
<Flavors>
<Name>Strawberr y</Name>
</Flavors>
<StartDate>2007 0804</StartDate>
<EndDate>200712 31</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 13392
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:styleshe et
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(Sta rtDate, '_',
EndDate)"/>

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

<xsl:template match="Flavor" mode="group">
<Group>
<Name><xsl:valu e-of select="concat( 'Group', position())"/></Name>
<Flavors>
<xsl:apply-templates select="key('by-date', concat(StartDat e,
'_', EndDate))/Name"/>
</Flavors>
<xsl:apply-templates select="StartDa te | 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
1542
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 records and produce output. Example <registrations> <registration> <company>Awesome Printers</company> <first>John</first>
4
1835
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
1608
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 the corresponding answers? I don't want to retrieve all the Answer and VoteAnswers nodes <xsl:for-each select="NSurveyVoter/Voter/Question/Answer"> <xsl:value-of select="Answer/Answer/text()"/>
1
3728
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
2
1664
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 to figure out how to generate a tranform that will create an HTML form from the schema section and populate the controls with the values from the data section (if present). I _think_ I'd be able to come up with the transform to generate
1
1210
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> <Name>Vanilla</Name> <StartDate>20070704</StartDate> <EndDate>20071231</EndDate> </Flavor>
4
7132
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" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"> <xsl:output method="html" encoding="UTF-8"/>
2
22785
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 in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most common usage of this is in creating hyperlinks.
1
5724
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" project="neptune"/> <file name="potrzebie.dbf" size="1102" project="jupiter"/>
2
2001
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 line 142 (line 12 in snippet) the error is caused by this call: empty($inSeite_inp->getValue('PAR_NAME')) method definition see second snippet, lines 142 to 166
0
11114
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10730
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...
1
10835
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10407
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
9563
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
7114
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
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4605
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
2
4205
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.