René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

XSL: Sorting twice

This is an extension of this sorting example.
The XML file:
<?xml version="1.0" encoding="UTF-8"?>

<persons>
   
   <person>
     <firstname>Edward</firstname>
     <lastname>Jenner</lastname>
    </person>
    
    <person>
     <firstname>Gertrud</firstname>
     <lastname>Elion</lastname>
    </person>
   
   <person>
     <firstname>Charles</firstname>
     <lastname>Babbager</lastname>
    </person>
    
     <person>
     <firstname>Alan</firstname>
     <lastname>Touring</lastname>
    </person>
    
    <person>
     <firstname>Ada</firstname>
     <lastname>Byron</lastname>
    </person>
       
   <person>
     <firstname>Tycho</firstname>
     <lastname>Brahe</lastname>
    </person>
    
     <person>
     <firstname>Johannes</firstname>
     <lastname>Kepler</lastname>
    </person>
    
    <person>
     <firstname>Galileo</firstname>
     <lastname>Galilei</lastname>
    </person>
       
</persons>
The XSL file:
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

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


<xsl:template match="persons">
  <h1>Lastname</h1>

  <table>
   <xsl:apply-templates select="person">
     <xsl:sort select="lastname"/>
   </xsl:apply-templates>
  </table>

  <h1>Firstname Vorname</h1>

  <table>
   <xsl:apply-templates select="person">
     <xsl:sort select="firstname"/>
   </xsl:apply-templates>
  </table>

</xsl:template>

<xsl:template match="person">
  <xsl:choose>

    <xsl:when test="position() mod 2 = 0">
      <xsl:text disable-output-escaping="yes">
        &lt;tr style='background-color="#cceeee"'&gt;
      </xsl:text>
    </xsl:when>

    <xsl:otherwise>
      <xsl:text disable-output-escaping="yes">
        &lt;tr style='background-color="#eeffcc"'&gt;
      </xsl:text>
    </xsl:otherwise>

  </xsl:choose>

  <td><xsl:value-of select="firstname" /></td>
  <td><xsl:value-of select="lastname" /></td>

  <xsl:text disable-output-escaping="yes">
    &lt;/tr&gt;
  </xsl:text>

</xsl:template>
</xsl:stylesheet>

The output (whitespaces manually modified):