xsl-list
[Top] [All Lists]

Re: [xsl] Sorting things on two levels

2015-04-14 11:38:39
Michele R Combs mrrothen(_at_)syr(_dot_)edu 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.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

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