xsl-list
[Top] [All Lists]

Re: XSLT 1.0 Grouping

2005-04-11 22:51:43
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(_at_)optonline(_dot_)net> 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




--~------------------------------------------------------------------
XSL-List info and archive: 
http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to:
http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>