[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Creating unique groups

Subject: Re: Creating unique groups
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Mon, 6 Dec 2004 20:39:59 -0800 (PST)
xsl for each distinct values
It is also worth exploring the XSLT 2.0 grouping
features. Following is the XSLT 2.0 answer (which
eliminates duplicates as well).  

<?xml version="1.0"?> 
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
 
<xsl:output method="xml" indent="yes" /> 

<xsl:template match="/root">
  <root>
    <xsl:for-each-group select="group"
group-by="@name">
      <group name="{@name}">
        <xsl:variable name="curr-group"
as="element(entry)*">
          <xsl:copy-of select="current-group()/*" />
        </xsl:variable>
        <xsl:for-each
select="distinct-values($curr-group/@name)">
          <entry name="{.}" />
        </xsl:for-each>
      </group>  
     </xsl:for-each-group>
  </root> 
</xsl:template>
   
</xsl:stylesheet>

The stylesheet is tested with Saxon 8.1..

Regards,
Mukul

--- Dave Roe <david_roe@xxxxxxx> wrote:

> Fantastic - that certainly does the trick, thank
> you.
> 
> Is there a way of extending this approach so that it
> can de-duplicate 
> entries?
> 
> For example, if I have:
> 
> <root>
> 
> 	<group name="group1">
> 		<entry name="entry1" />
> 		<entry name="entry2" />
> 	</group>
> 
> 	<group name="group1">
> 		<entry name="entry2" />
> 		<entry name="entry3" />
> 	</group>
> 
> </root>
> 
> and I want to end up with:
> 
> <root>
> 
> 	<group name="group1">
> 		<entry name="entry1" />
> 		<entry name="entry2" />
> 		<entry name="entry3" />
> 	</group>
> 
> </root>
> 
> (with producing 2 entries for entry2).
> 
> Thanks again,
> /dave
> 
> On Dec 3, 2004, at 11:29 PM, Mukul Gandhi wrote:
> 
> > Hi Dave,
> >   Please try this XSL. It uses the Muenchian
> > technique.
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <xsl:stylesheet
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> > version="1.0">
> >
> > <xsl:key name="by-group-name" match="group"
> > use="@name" />
> >
> > <xsl:template match="/root">
> >   <root>
> >     <xsl:for-each select="group[generate-id(.) =
> > generate-id(key('by-group-name', @name)[1])]">
> >       <group name="{@name}">
> >         <xsl:for-each select="key('by-group-name',
> > @name)">
> >           <xsl:copy-of select="*"/>
> >         </xsl:for-each>
> >       </group>
> >     </xsl:for-each>
> >   </root>
> > </xsl:template>
> >
> > </xsl:stylesheet>
> >
> > Regards,
> > Mukul



		
__________________________________ 
Do you Yahoo!? 
All your favorites on one personal page  Try My Yahoo!
http://my.yahoo.com 

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.