Re: [xsl] Only first rows (after the sort)

Subject: Re: [xsl] Only first rows (after the sort)
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Tue, 15 Aug 2006 21:19:28 +0530
Hi Dennis,
  If you solve this problem using Muenchian method (as explained by
Wendell), it'll be very efficient.

But here is a solution, which is not efficient as the Muenchian
method, but this is probably how a layman would think to solve this
problem. The logic is quite simple, and only needs one to know how
preceding-sibling axis works.

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

<xsl:output method="html" indent="yes" />

<xsl:template match="/examples">
  <html>
    <head>
      <title/>
    </head>
    <body>
      <table>
        <tr>
          <th>Catagory</th>
          <th>Year</th>
        </tr>
        <xsl:for-each select="example[not(concat(category, year) =
concat(preceding-sibling::example/category,
preceding-sibling::example/year))]">
          <xsl:sort select="category" />
          <xsl:sort select="year" data-type="number" />
          <tr>
            <td><xsl:value-of select="category" /></td>
            <td><xsl:value-of select="year" /></td>
          </tr>
        </xsl:for-each>
      </table>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

This when applied to XML:

<examples>
 <example>
  <category>XML</category>
  <year>2005</year>
 </example>
 <example>
  <category>XSL</category>
  <year>2005</year>
 </example>
 <example>
  <category>XSL</category>
  <year>2006</year>
 </example>
 <example>
  <category>XML</category>
  <year>2005</year>
 </example>
</examples>

Produces output:

<html>
  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title></title>
  </head>
  <body>
     <table>
        <tr>
           <th>Catagory</th>
           <th>Year</th>
        </tr>
        <tr>
           <td>XML</td>
           <td>2005</td>
        </tr>
        <tr>
           <td>XSL</td>
           <td>2005</td>
        </tr>
        <tr>
           <td>XSL</td>
           <td>2006</td>
        </tr>
     </table>
  </body>
</html>

On 8/15/06, Stinissen, Dennis <D.Stinissen@xxxxxxxxx> wrote:
Hi,

I have a problem transforming an XML file to the desired table
structure. I hope someone can help me.

Here is the XML file I use:
<example>
 <category>XML</category>
 <year>2005</year>
</example>
<example>
 <category>XSL</category>
 <year>2005</year>
</example>
<example>
 <category>XSL</category>
 <year>2006</year>
</example>
<example>
 <category>XML</category>
 <year>2005</year>
</example>
...

Here is how the table should look eventually:
Catagory    Year
XML         2005
XSL         2005
XSL         2006
...

My current XSL:

<xsl:for-each select="example">
<xsl:sort select="category"><xsl:sort select="year">
<tr><td><xsl:value-of select="./category"></td><td><xsl:value-of
select="./year"></td></tr>
</xsl:for-each>

This results in:
Catagory    Year
XML         2005
XML         2005
XSL         2005
XSL         2006
...

So what I'm trying to do is to skip rows when the combination of
category and year are identical  (skip the duplicates). I've tried to
put it in an <xsl:if> combined with <xsl:variable> construction, but I
just can't get it right. Does anyone have an idea?

Kind regards,
Dennis

-- Regards, Mukul Gandhi

http://gandhimukul.tripod.com

Current Thread