%META:TOPICINFO{author="PatrickDiamond" date="1099671808" format="1.0" version="1.8"}%
%META:TOPICPARENT{name="XMLQueryPlugin"}%
---+ Recipe showing 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 |


---++Transformed Table
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%

---++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>

