Re: [xsl] grouping, sorting, splitting

Subject: Re: [xsl] grouping, sorting, splitting
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Mon, 18 Apr 2005 21:00:54 -0700 (PDT)
I found another bug in my stylesheet.. (there was a
wrong <tr> tag appearing).. 

Following is the modified stylesheet -

<?xml version="1.0"?> 
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

<xsl:output method="html" indent="yes" />
  
<xsl:key name="by-date" match="entry" use="@date" />
  
<xsl:template match="/page">
    <html>
      <head>
        <title/>
      </head>
      <body>
        <table>
          <xsl:for-each select="entry[generate-id() =
generate-id(key('by-date', @date)[1])]">
            <xsl:for-each select="key('by-date',
@date)">
              <xsl:sort select="title" />
              <xsl:if test="(position() = 1) or
((position() - 1) mod 3 = 0)">
                <xsl:variable name="pos"
select="position()" />                
                <xsl:call-template name="generateTRs">
                  <xsl:with-param name="node-set"
select="key('by-date', @date)[position() &gt;=
$pos][position() &lt;= ($pos + 2)]" />
                </xsl:call-template>                
              </xsl:if>
            </xsl:for-each>
            <!-- a dummy row -->
            <tr>
              <td>-</td><td>-</td><td>-</td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>  
</xsl:template>
   
<xsl:template name="generateTRs">
    <xsl:param name="node-set" />
    
    <tr>
      <xsl:for-each select="$node-set">
        <td>
          <xsl:value-of select="title" />
        </td>
      </xsl:for-each> 
      <xsl:call-template name="generateRemainingTDs">
         <xsl:with-param name="n" select="3 -
count($node-set)" />
      </xsl:call-template>
    </tr>  
</xsl:template>
  
<xsl:template name="generateRemainingTDs">
    <xsl:param name="n" />
        
    <xsl:if test="$n &gt; 0">
      <td/>
      <xsl:call-template name="generateRemainingTDs">
        <xsl:with-param name="n" select="$n - 1" />
      </xsl:call-template>
    </xsl:if>
</xsl:template>
  
</xsl:stylesheet>

For e.g., when it is applied to XML -

<page>
  <entry date="2005-04-15">
    <title>foo</title>
  </entry>
  <entry date="2005-04-15">
    <title>bar</title>
  </entry>
  <entry date="2005-04-15">
    <title>baz</title>
  </entry>
  <entry date="2004-04-15">
    <title>a</title>
  </entry>
  <entry date="2004-04-15">
    <title>b</title>
  </entry>
  <entry date="2004-02-05">
    <title>c</title>
  </entry>
  <entry date="2003-04-15">
    <title>d</title>
  </entry>
  <entry date="2003-04-15">
    <title>e</title>
  </entry>
  <entry date="2003-02-05">
    <title>f</title>
  </entry>
  <entry date="2002-02-05">
    <title>g</title>
  </entry>
</page>

The output produced is -

<html>
   <head>
      <meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
      <title></title>
   </head>
   <body>
      <table>
         <tr>
            <td>foo</td>
            <td>bar</td>
            <td>baz</td>
         </tr>
         <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
         </tr>
         <tr>
            <td>a</td>
            <td>b</td>
            <td></td>
         </tr>
         <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
         </tr>
         <tr>
            <td>c</td>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
         </tr>
         <tr>
            <td>d</td>
            <td>e</td>
            <td></td>
         </tr>
         <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
         </tr>
         <tr>
            <td>f</td>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
         </tr>
         <tr>
            <td>g</td>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>-</td>
            <td>-</td>
            <td>-</td>
         </tr>
      </table>
   </body>
</html>

Regards,
Mukul


--- David Carlisle <davidc@xxxxxxxxx> wrote:
> > 
> Actually we just said that it's better (for your
> brain, if not for your
> computer) to do it in two passes. I did explictly
> note that since the
> sorting criterion is pretty simple in this case, you
> could probaby do it
> in one pass.
> 
> David
> 
>
________________________________________________________________________
> This e-mail has been scanned for all viruses by
> Star. The
> service is powered by MessageLabs. For more
> information on a proactive
> anti-virus service working around the clock, around
> the globe, visit:
> http://www.star.net.uk
>
________________________________________________________________________
> 
> 


		
__________________________________ 
Do you Yahoo!? 
Plan great trips with Yahoo! Travel: Now over 17,000 guides!
http://travel.yahoo.com/p-travelguide

Current Thread