[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Sorting things on two levels


Subject: Re: [xsl] Sorting things on two levels
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 14 Apr 2015 16:38:34 -0000

Michele R Combs mrrothen@xxxxxxx wrote:

Looks like grouping and sorting to me. Do you use XSLT 1.0 or 2.0?


I'll be running the transform using the latest version of Oxygen, so it looks like either would be fine. From the Oxygen Help file:

With XSLT 2.0 here is an approach:


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">


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

<xsl:template match="root">
<ul>
<xsl:for-each-group select="indexterm" group-by="primary">
<xsl:sort select="current-grouping-key()"/>
<li>
<xsl:value-of select="current-grouping-key(), if (not(current-group()/secondary)) then current-group()/@id else ()" separator=", "/>
<xsl:if test="current-group()/secondary">
<ul>
<xsl:for-each-group select="current-group()" group-by="secondary">
<xsl:sort select="current-grouping-key()"/>
<li>
<xsl:value-of select="current-grouping-key(), current-group()/@id" separator=", "/>
</li>
</xsl:for-each-group>
</ul>
</xsl:if>
</li>
</xsl:for-each-group>
</ul>
</xsl:template>


</xsl:stylesheet>

Outputs

<ul>
   <li>aardvarks, {ID}</li>
   <li>bears
      <ul>
         <li>breeding, {ID}</li>
         <li>feeding, {ID}</li>
         <li>hunting, {ID}, {ID}</li>
      </ul>
   </li>
</ul>

for your sample data.


Current Thread
Keywords