[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Output sorted XHTML table for a subset of elements


Subject: Re: [xsl] Output sorted XHTML table for a subset of elements
From: Steven Davies <xsl-list@xxxxxxxxxxx>
Date: Sun, 25 May 2008 11:33:29 +0100

Hi,

thanks for the suggestion but there's an extra level of complexity I may
not have explained too well - I need the output table to always contain
 5 elements per row (or fewer on the final row), and this is the bit I
was having problems with!

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>

(assuming I copied the values down by hand correctly!)

Thanks for any further help!

Steve

Erik Vullings wrote:
Hi,

I'm not familiar with your XSLT library, but the following XSLT worked for me:

<?xml version="1.0"?>
<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:for-each>
               </tr>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Cheers
Erik


On Sat, May 24, 2008 at 4:45 PM, Steven Davies <xsl-list@xxxxxxxxxxx> wrote:
Hi,

I've been having a problem with a stylesheet I'm creating and need some
help with a part of it - given the following XML snippet:

<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>
</users>

..what I'm trying to achieve is an XHTML table with 5 columns and
however many rows are necessary (imagine there may be a hundred or so
users) where the items in the table consist of only the users where the
user's line count is above 25 and is sorted by their line count.

So for the above snippet the output should be:

<table>
 <tr>
   <td>charlie (731)</td>
   <td>bert (78)</td>
   <td>derek (62)</td>
 </tr>
</table>

..obviously with more columns per row and more rows depending on the
size of the input document.

I've tried to do this using a template matching the next-siblings of the
users to achieve the grouping, but then I can't sort the items or select
the ones I want because the XPath works on the input node set and not a
node set I've selected myself (using for-each or a template).

If it makes any difference I'm using libXSLT 1.1.22 to do the
transformation.

Can anyone give me any pointers with this?

Thanks,
Steve


Current Thread
Keywords