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

Re: Output sorted XHTML table for a subset of elements

Subject: Re: Output sorted XHTML table for a subset of elements
From: "Erik Vullings" <erik.vullings@xxxxxxxxx>
Date: Mon, 26 May 2008 00:16:10 +0200
Re:  Output sorted XHTML table for a subset of elements
Another v1.0 example, but without extensions:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" encoding="Windows-1252" />
   <xsl:template match="/users">
      <html>
         <body>
            <table>
               <tr>
                  <xsl:for-each select="user">
                     <xsl:sort select="lines" data-type="number"
order="descending"/>
                     <xsl:if test = "lines > 25">
                     	<td><xsl:value-of select="@name"
/>(<xsl:value-of select="lines" />)</td>
                     </xsl:if>
                     <xsl:if test="position() mod 5 = 0">
                     	<xsl:text disable-output-escaping =
"yes">&#x0D;&#x0A;&lt;/tr&gt;&#x0D;&#x0A;</xsl:text>
                     	<xsl:text disable-output-escaping =
"yes">&lt;tr&gt;&#x0D;&#x0A;</xsl:text>
                     </xsl:if>
                  </xsl:for-each>
               </tr>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

The </tr><tr> bit is rather ugly, but I find the test="position() mod
5 = 0" rather elegant.

Cheers
Erik

On Sun, May 25, 2008 at 12:53 PM, Martin Honnen <Martin.Honnen@xxxxxx> wrote:
>
> Steven Davies wrote:
>
>> I know my sample data was a bit small but there are a lot more nodes in
>> my actual data set. Here's an attempt at a better example:
>>
>> <users>
>>  <user name="alf"><lines>7</lines></user>
>>  <user name="bert"><lines>78</lines></user>
>>  <user name="charlie"><lines>731</lines></user>
>>  <user name="derek"><lines>62</lines></user>
>>  <user name="edward"><lines>93</lines></user>
>>  <user name="fred"><lines>823</lines></user>
>>  <user name="george"><lines>42</lines></user>
>>  <user name="harry"><lines>28</lines></user>
>>  <user name="ian"><lines>553</lines></user>
>>  <user name="joshua"><lines>92</lines></user>
>>  <user name="kevin"><lines>108</lines></user>
>>  <user name="luke"><lines>192</lines></user>
>> </users>
>>
>> should give the output:
>>
>> <table>
>>  <tr>
>>    <td>fred (823)</td>
>>    <td>charlie (731)</td>
>>    <td>ian (553)</td>
>>    <td>luke (192)</td>
>>    <td>kevin (108)</td>
>>  </tr>
>>  <tr>
>>    <td>edward (93)</td>
>>    <td>joshua (92)</td>
>>    <td>bert (78)</td>
>>    <td>derek (62)</td>
>>    <td>george (42)</td>
>>  </tr>
>>  <tr>
>>    <td>harry (28)</td>
>>  </tr>
>> </table>
>
> Here is an XSLT 1.0 stylesheet making use of exslt:node-set:
>
> <xsl:stylesheet
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>  xmlns:exsl="http://exslt.org/common"
>  exclude-result-prefixes="exsl"
>  version="1.0">
>
>  <xsl:output method="html" indent="yes"/>
>
>  <xsl:param name="cols" select="5"/>
>
>  <xsl:param name="min-lines" select="25"/>
>
>  <xsl:template match="/">
>    <html>
>      <head>
>        <title>Example</title>
>      </head>
>      <body>
>        <xsl:apply-templates select="users"/>
>      </body>
>    </html>
>  </xsl:template>
>
>  <xsl:template match="users">
>    <xsl:variable name="sorted-users-rtf">
>      <data>
>        <xsl:for-each select="user[lines &gt; $min-lines]">
>          <xsl:sort select="lines" data-type="number" order="descending"/>
>          <xsl:copy-of select="."/>
>        </xsl:for-each>
>      </data>
>    </xsl:variable>
>    <xsl:variable name="sorted-users" select="exsl:node-set($sorted-users-rtf)/data/user"/>
>    <table>
>      <tbody>
>        <xsl:apply-templates select="$sorted-users[position() mod $cols = 1]" mode="row"/>
>      </tbody>
>    </table>
>  </xsl:template>
>
>  <xsl:template match="user" mode="row">
>    <tr>
>      <xsl:apply-templates select=". | following-sibling::user[position() &lt; $cols]" mode="cell"/>
>    </tr>
>  </xsl:template>
>
>  <xsl:template match="user" mode="cell">
>    <td>
>      <xsl:value-of select="concat(@name, ' (', lines, ')')"/>
>    </td>
>  </xsl:template>
>
> </xsl:stylesheet>
>
> I think libXSLT supports exsl:node-set.
>
>
> --
>
>        Martin Honnen
>        http://JavaScript.FAQTs.com/

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.