Erik
Erik

Reputation: 954

Sorting a table in XSLT 2.0

I have an index file, containing a list of xml files to process:

<?xml version="1.0" encoding="UTF-8"?>
<list>
<part>
      <numberGroup format="1" start="1">
    <entry>
      <file>04.xml</file>
          <title>first file</title>
          <css>stylesheet.css</css>
    </entry>
    <entry>
        <file>05.xml</file>
            <title>second file</title>
            <css>stylesheet.css</css>
    </entry>
    <entry>
    <file>06.xml</file>
            <title>third file</title>
            <css>stylesheet.css</css>
        </entry>
        .... more files
    </numberGroup>
    .... more NumberGroups
  </part>
  ....more parts
</list>

every file 01.xml etc has one HTML-style table, like this:

<table class='wl'>
<tr class='header'>
   <td><p>English</p></td>
   <td><p>French</p></td>
</tr>
<tr>
   <td><p>belly</p></td>
   <td><p>ventre</p></td>
</tr>
<tr>
   <td><p>leg</p></td>
   <td><p>jambe</p>/td>
</tr>
... etc
</table>

I want to merge all the tables (3 in this example) into one, so I get a compete vocabulary.

So far I have this stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
   <xsl:result-document href="wl.xml">
       <xsl:text disable-output-escaping="yes">
&lt;!DOCTYPE html&gt;
       </xsl:text>
       <html xmlns:epub="http://www.idpf.org/2007/ops" lang="nl" xml:lang="nl">
    <head>
      <title>
        <xsl:value-of select="./title"/>
      </title>
      <xsl:apply-templates select="css"/>
        </head>
    <body>
    <table>
       <xsl:apply-templates select="//numberGroup[1]/entry">
       </xsl:apply-templates>
    </table>
    </body>
    </html>
  </xsl:result-document>
  </xsl:template>
  <xsl:template match="entry">
<xsl:apply-templates select="document(file)//table[@class='wl']/tr[not(contains(@class, 'header'))]"/>
</xsl:template>
<xsl:template match="tr">
     <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

which merges as desired, except for the sorting. How can I sort the generated table alphabetically, on the first column?

Upvotes: 1

Views: 60

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

Replace

<xsl:apply-templates select="//numberGroup[1]/entry">
       </xsl:apply-templates>

with

<xsl:apply-templates select="document(//numberGroup[1]/entry/file)//table[@class='wl']/tr[not(contains(@class, 'header'))]">
  <xsl:sort select="td[1]" data-type="text"/>
       </xsl:apply-templates>

then you can drop the template for entry elements.

Upvotes: 2

Related Questions