[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sorting and gouping

Subject: Re: Sorting and gouping
From: Navratil Zdenek <z.navratil@xxxxxxxxxxxx>
Date: Wed, 31 Oct 2001 11:49:35 +0100
gouping
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.