[xsl] Re: Sorting and gouping

Subject: [xsl] Re: Sorting and gouping
From: Navratil Zdenek <z.navratil@xxxxxxxxxxxx>
Date: Wed, 31 Oct 2001 11:49:35 +0100
Ronald's code was extremely helpful for me. My problem was somewhat more
complicated. I'm terribly sorry for I forgot to emphasize that I needed
to enclose each group into tags (say DIV) i.e. <DIV id="...">
group_of_item_elements </DIV>. 
The problem is one can't create starting and ending tags in different
conditional clauses (error: "End tag 'xsl:when' does not match the start
tag 'DIV'" or so). To overcome this obstacle I generate DIV tags via
<xsl:text> with output escaping  disabled. Ronald's code with this patch
follows:

XML:
<?xml version="1.0"?>
<list>
    <item at="d">d</item>
    <item at="e">e</item>
    <item at="i">i</item>
    <item at="k">k</item>
    <item at="l">l</item>
    <item at="a">a</item>
    <item at="b">b</item>
    <item at="c">c</item>
    <item at="j">j</item>
    <item at="f">f</item>
    <item at="g">g</item>
    <item at="h">h</item>
    <item at="a">a</item>
    <item at="b">b</item>
    <item at="c">c</item>
    <item at="f">f</item>
</list>

XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="html"/>
  <xsl:param name="group">5</xsl:param> <!-- grouping size-->
  <xsl:variable name="items" select="count(//item)"/> <!-- total number
of items-->

  <xsl:template match="list">
    <HTML>
    <BODY>
      <xsl:apply-templates select="item">
        <xsl:sort select="@at" order="ascending"/>
      </xsl:apply-templates>
    </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="item">
    <xsl:choose>
      <xsl:when test="position() mod $group =1">
        <!-- Begin new group with gn = group # -->
        <xsl:variable name="gn" select="concat('g', (position() + 4) div
5)"/>
        <!-- DIV id="g#" -->
        <xsl:text disable-output-escaping="yes">&lt;DIV id="</xsl:text>
          <xsl:value-of select="$gn"/><xsl:text
disable-output-escaping="yes">"&gt;</xsl:text>
        <p><xsl:value-of select="position()"/>
          <xsl:apply-templates/></p>
      </xsl:when>
      <xsl:when test="position() mod $group =0">
        <!-- End group -->
        <p><xsl:value-of select="position()"/>
          <xsl:apply-templates/></p>
        <!-- /DIV -->
        <xsl:text
disable-output-escaping="yes">&lt;/DIV&gt;</xsl:text><hr />
      </xsl:when>
      <xsl:when test="not(position() mod $group =0) and position() =
$items"> 
        <!-- last item in sorted list-->
        <p><xsl:value-of select="position()"/>
          <xsl:apply-templates/></p>
        <!-- /DIV -->
        <xsl:text disable-output-escaping="yes">&lt;/DIV&gt;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <p><xsl:value-of select="position()"/>
          <xsl:apply-templates/></p>
      </xsl:otherwise>		
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Regards,
 Zdenek

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread