RE: [xsl] XSLT 1.0 Grouping

Subject: RE: [xsl] XSLT 1.0 Grouping
From: aspsa <aspsa@xxxxxxxxxxxxx>
Date: Tue, 12 Apr 2005 03:08:30 -0400
Thanks for your reply, Mukul. Your solution works. In the meanwhile, I came
up with the following if you are interested.

-----

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

 <xsl:key name="byAge" match="person" use="age"/>

 <xsl:template match="/">
  <people>
   <xsl:apply-templates mode="personGrouped"
select="root/people/person[generate-id()=generate-id(key('byAge',
age)[1])]">
    <xsl:sort select="age" data-type="number" order="descending"/>
   </xsl:apply-templates>
  </people>

 </xsl:template>

 <xsl:template match="person" mode="personGrouped">
  <age>
   <xsl:attribute name="value"><xsl:value-of select="age"/></xsl:attribute>
   <xsl:apply-templates mode="personDetails" select="key('byAge', age)">
    <xsl:sort select="name/lname" data-type="text" order="ascending"/>
   </xsl:apply-templates>
  </age>
 </xsl:template>

 <xsl:template match="person" mode="personDetails">
  <name>
   <fname><xsl:value-of select="name/fname"/></fname>
   <mname><xsl:value-of select="name/mname"/></mname>
   <lname><xsl:value-of select="name/lname"/></lname>
  </name>
 </xsl:template>

</xsl:stylesheet>

-----

Thanks again.


Regards,

ASP

-----Original Message-----
From: Mukul Gandhi [mailto:mukul_gandhi@xxxxxxxxx]
Sent: Tuesday, April 12, 2005 1:52 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] XSLT 1.0 Grouping


Please try this XSL..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

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

<xsl:key name="byAge" match="person" use="age"/>

<xsl:template match="/root/people">
   <people>
     <xsl:for-each select="person[generate-id() =
generate-id(key('byAge', age)[1])]">
       <xsl:sort select="age" order="ascending"
data-type="number" />
       <age value="{age}">
         <xsl:copy-of select="key('byAge', age)/name"
/>
       </age>
     </xsl:for-each>
   </people>
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

--- aspsa <aspsa@xxxxxxxxxxxxx> wrote:
> Hi, folks...
>
> I'm trying to wrap my brain around the Muenchian
> Method. Essentially, I have
> the following simple XML document.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>  <people>
>   <person>
>    <name>
>     <fname>Jack</fname>
>     <mname>Fred</mname>
>     <lname>Smith</lname>
>    </name>
>    <age>22</age>
>   </person>
>   <person>
>    <name>
>     <fname>Jane</fname>
>     <mname>Mary</mname>
>     <lname>Smith</lname>
>    </name>
>    <age>23</age>
>   </person>
>   <person>
>    <name>
>     <fname>Frank</fname>
>     <mname>Joseph</mname>
>     <lname>Franks</lname>
>    </name>
>    <age>23</age>
>   </person>
>  </people>
> </root>
>
> I'd like to sort and group it by age, such that each
> person of the same age
> is placed in ascending order within each unique age
> value.
>
> Here's what I have so far.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
>  <xsl:output method="xml" version="1.0"
> encoding="UTF-8" indent="yes"/>
>
>  <xsl:key name="byAge" match="person" use="age"/>
>
>  <xsl:template match="/">
>   <people>
>    <xsl:apply-templates select="root/people/person">
>     <xsl:sort select="age" data-type="number"
> order="ascending"/>
>    </xsl:apply-templates>
>    <xsl:apply-templates mode="personGrouped"
>
>
select="root/people/person[generate-id()=generate-id(key('byAge',
> age)[1])]"/>
>   </people>
>  </xsl:template>
>
>  <xsl:template match="person" mode="personGrouped">
>   <age>
>    <xsl:attribute name="value"><xsl:value-of
> select="age"/></xsl:attribute>
>    <name>
>     <fname><xsl:value-of
> select="name/fname"/></fname>
>     <mname><xsl:value-of
> select="name/mname"/></mname>
>     <lname><xsl:value-of
> select="name/lname"/></lname>
>    </name>
>   </age>
>  </xsl:template>
>
> </xsl:stylesheet>
>
> Here's the output after processing...
>
> <?xml version="1.0" encoding="UTF-8"?>
>
<people>JackFredSmith22JaneMarySmith23FrankJosephFranks23
>  <age value="22">
>   <name>
>    <fname>Jack</fname>
>    <mname>Fred</mname>
>    <lname>Smith</lname>
>   </name>
>  </age>
>  <age value="23">
>   <name>
>    <fname>Jane</fname>
>    <mname>Mary</mname>
>    <lname>Smith</lname>
>   </name>
>  </age>
> </people>
>
> Here is the output I would like to see...
>
> <?xml version="1.0" encoding="UTF-8"?>
> <people>
>  <age value="22">
>   <name>
>    <fname>Jack</fname>
>    <mname>Fred</mname>
>    <lname>Smith</lname>
>   </name>
>  </age>
>  <age value="23">
>   <name>
>    <fname>Frank</fname>
>    <mname>Joseph</mname>
>    <lname>Franks</lname>
>   </name>
>   <name>
>    <fname>Jane</fname>
>    <mname>Mary</mname>
>    <lname>Smith</lname>
>   </name>
>  </age>
> </people>
>
> Thanks for your feedback.
>
>
> Regards,
>
> ASP
>
>



__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/

Current Thread