Re: [xsl] Grooping and Sorting problem

Subject: Re: [xsl] Grooping and Sorting problem
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 28 Mar 2002 15:31:44 -0500
At 2002-03-28 14:14 -0500, Frédéric Tremblay wrote:
> Hi, i want to make a HTML table with 5 column and n rows with a XML like
> this:
>
> <root>
>       <test number="3">
>       <test number="1">
>       <test number="4">
>       <test number="2">
>       <test number="5">
>       ...
>       <test number="100">
> </root>
>
> so i want to make a table with 5 column and n rows, at every 5 element
> i want a new row.
>
> BUT THE PROBLEM IS: I NEED TO HAVE ALL ELEMENT SORTED!!!
>
> How can i do this?

In two steps ... first do the sort and then the containerization into rows.


Because axes are not changed when sorting the node list, I'm not sure what you need can be done recursively in a single pass. At least I haven't tried very hard because the two-pass works quickly.

I hope this helps.

........................ Ken


t:\ftemp>type fred.xml <root> <test number="3"/> <test number="1"/> <test number="4"/> <test number="2"/> <test number="5"/> <test number="8"/> <test number="9"/> <test number="13"/> <test number="21"/> <test number="34"/> <test number="12"/> <test number="45"/> <test number="100"/> </root>

t:\ftemp>type fred-int.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="/root">
  <root>
    <xsl:for-each select="test">
      <xsl:sort select="@number" data-type="number"/>
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </root>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>type fred.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="/">
  <html>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

<xsl:template match="root">
  <table>
    <xsl:for-each select="test[1]"><!--establish the current node list-->
      <xsl:call-template name="do-a-row"/>
    </xsl:for-each>
  </table>
</xsl:template>

<!--containerize the groups of columns in a single row-->
<xsl:template name="do-a-row">
  <tr>
    <xsl:call-template name="do-a-col"/>
  </tr>
  <xsl:for-each select="following-sibling::test[5]">
    <xsl:call-template name="do-a-row"/>
  </xsl:for-each>
</xsl:template>

<xsl:template name="do-a-col">
  <xsl:param name="index" select="5"/>
  <xsl:if test="$index">
    <td>
      <xsl:value-of select="@number"/>
    </td>
    <xsl:for-each select="following-sibling::test[1]">
      <xsl:call-template name="do-a-col">
        <xsl:with-param name="index" select="$index - 1"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon -o fred-int.xml fred.xml fred-int.xsl

t:\ftemp>saxon -o fred.htm fred-int.xml fred.xsl

t:\ftemp>type fred.htm
<html>
   <body>
      <table>
         <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
         </tr>
         <tr>
            <td>8</td>
            <td>9</td>
            <td>12</td>
            <td>13</td>
            <td>21</td>
         </tr>
         <tr>
            <td>34</td>
            <td>45</td>
            <td>100</td>
         </tr>
      </table>
   </body>
</html>


-- Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO: June 17-21, 2002 - : 3-days XML Information Modeling: July 31-August 2, 2002

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:   2002-04-08,09,10,11,05-06,07,09,10,13,20,
-                                06-04,07,10,11,13,14,17,20,07-31


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



Current Thread