%META:TOPICINFO{author="TWikiContributor" date="1302480031" format="1.1" version="1"}%
%META:TOPICPARENT{name="XMLQueryPlugin"}%
---+!! !XmlQueryPlugin Recipe 3: Transform Tables

XmlQueryPlugin Recipe 3 shows how to Transform a table or set of tables.

%TOC%

---++Sample Tables

| *Author* | *Title* |
| Rider, Henry | Allan and the Holy Flower |
| Rider, Henry | Allan Quatermain |
| Rider, Henry | Allan's Wife |
| Rider, Henry | The Ancient Allan |
| Campan, Jeanne Louise Henriette | Images from Campan's Marie Antoinette |
| Campan, Jeanne Louise Henriette | Marie Antoinette Volume 01 |
| Campan, Jeanne Louise Henriette | Marie Antoinette Volume 02 |
| Casanova, Giacomo |Memoirs of Casanova Volume 01: Childhood |
| Casanova, Giacomo |Memoirs of Casanova Volume 02: a Cleric in Naples |
| Casanova, Giacomo |Memoirs of Casanova Volume 03: Military Career |
| Casanova, Giacomo |Memoirs of Casanova Volume 04: Return to Venice |

| *Author* | *Title* |
| Poe, Edgar Allan | Alone |
| Poe, Edgar Allan | Derniers Contes|
| Poe, Edgar Allan | Edgar Allan Poe's Complete Poetical Works |
| Poe, Edgar Allan | The Fall of the House of Usher |

---++Annotated Query

<verbatim>
%XSLTSTART{id="%WEB%" benchmark="off" debug="off" }% 
 <xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
 <table border="1">
 <tr><th>Title</th><th>Author</th></tr>
 <xsl:apply-templates select="/twiki/web/topic/data/tables/table/row[position()=1]/field[@type='title' and position()=1 and text()='Author']/../../row">
</verbatim>
__Select each row from all tables that have the first field in the first row equal to 'Title' and of type 'title'. Then apply the template that matchs "row"__
<verbatim>
          <xsl:sort select="field[2]"/>
</verbatim>
__Sort each row according to the contents of the second child tag named field__
<verbatim>
 </xsl:apply-templates>
 </table>
</xsl:template>


<xsl:template match="row">
</verbatim>
__Executed from the apply-templates tag against each node in the select= list__
<verbatim>

         <xsl:if test="./field[1][@type='data']">
</verbatim>
__If this is a data field then output it__
<verbatim>

         <tr>                   
                 <td><xsl:value-of select="./field[2]"/></td>
                  <td><xsl:value-of select="./field[1]"/></td>
         </tr>
         </xsl:if>

  </xsl:template>
  </xsl:stylesheet>
%XSLTEND%
</verbatim>

---++ XSLT Demo Transformed Table
*XmlQuery must be Installed for this demo to work. The <nop>XmlQueryPugin is not installed properly if the %<nop>XSLTSTART{}% variable appears below*

The above tables has been transformed in 3 ways
        1 They have been combined
        1 The __Author__ and __Title__ columns have been swapped
        1 The entries have been sorted by __Title__

%XSLTSTART{id="%WEB%" benchmark="off" debug="off" }% 
 <xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
 <table border="1">
 <tr><th>Title</th><th>Author</th></tr>
 <xsl:apply-templates select="/twiki/web/topic/data/tables/table/row[position()=1]/field[@type='title' and position()=1 and text()='Author']/../../row">
          <xsl:sort select="field[2]"/>
 </xsl:apply-templates>
 </table>
</xsl:template>


<xsl:template match="row">
         <xsl:if test="./field[1][@type='data']">
         <tr>                   
                 <td><xsl:value-of select="./field[2]"/></td>
                  <td><xsl:value-of select="./field[1]"/></td>
         </tr>
         </xsl:if>

  </xsl:template>
  </xsl:stylesheet>
%XSLTEND%

-----
__Related:__ %SEARCH{ "XmlQueryPlugin" scope="topic" topic="XmlQueryPlugin*" excludetopic="%TOPIC%" nonoise="on" format="$topic" separator=", " }%
