%META:TOPICINFO{author="PatrickDiamond" date="1099671874" format="1.0" version="1.3"}%
%META:TOPICPARENT{name="XMLQueryPlugin"}%
---+ Recipe showing how to restructure a table or set of tables
%TOC%

---++Sample Table 

| *Title* | *Author* |
| Early Israel and the Surrounding Nations | Sayce, Archibald Henry |
| Early Kings of Norway | Carlyle, Thomas |
| Early Letters of George Wm. Curtis | Cooke, George Willis |
| Early Letters of George Wm. Curtis | Curtis, George William |
| The Early Life of Mark Rutherford (W. Hale White) | Rutherford, Mark |
| Early Plays Catiline, the Warrior's Barrow, Olaf Liljekrans | Ibsen, Henrik |
| Early Plays Catiline, the Warrior's Barrow, Olaf Liljekrans | Orbeck, Anders |
| The Early Poems of Alfred Lord Tennyson | Collins, John Churton |
| The Early Poems of Alfred Lord Tennyson | Tennyson, Alfred Lord |
| Early Short Fiction of Edith Wharton Audio Book | Wharton, Edith |
| The Parenticide Club | Bierce, Ambrose |
| The Parent's Assistant | Edgeworth, Maria |
| Paris as It Was and as It Is | Blagdon, Francis W. |
| The Parish Clerk (1907) | Ditchfield, P. H. (Peter Hampson) |
| Parish Papers | Macleod, Norman |
| The Parish Register | Crabbe, George |

---++ Table Restructured using XSLT

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

 <xsl:template match="/twiki">
 <xsl:for-each select="/twiki/web/topic/data/tables/table/row[position()=1]/field[@type='title' and position()=1 and text()='Title']">
	  <xsl:for-each select="../following-sibling::*">
			<table border="1">
			<tr><th>Key</th><th>Value</th></tr>
			<xsl:for-each select="field">
				 <xsl:variable name="current-position" select="position()"/>	
				 <tr>
					 <td><xsl:value-of select="../../row[position()=1]/field[position()=$current-position]"/></td>
					 <td><xsl:value-of select="."/></td>
				 </tr>
			</xsl:for-each>
	  </table>
	  </xsl:for-each>
  </xsl:for-each>

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

---++Annotated XSLT

<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="/twiki">
 <xsl:for-each select="/twiki/web/topic/data/tables/table/row[position()=1]/field[@type='title' and position()=1 and text()='Title']">
</verbatim>
__Foreach table with the first field in the first row being a header with the text 'Title'__
<verbatim>
	  <xsl:for-each select="../following-sibling::*">
</verbatim>
__Foreach row following the first one__
<verbatim>
			<table border="1">
			<tr><th>Key</th><th>Value</th></tr>
			<xsl:for-each select="field">
</verbatim>
__Foreach field tag within this row__
<verbatim>
				 <xsl:variable name="current-position" select="position()"/>	
</verbatim>
__Note the position of the current field__
<verbatim>
				 <tr>
					 <td><xsl:value-of select="../../row[position()=1]/field[position()=$current-position]"/></td>
</verbatim>
__Extract the title of this field going to the first row then the field with the same position as the current field__
<verbatim>
					 <td><xsl:value-of select="."/></td>
				 </tr>
			</xsl:for-each>
	  </table>
	  </xsl:for-each>
  </xsl:for-each>

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

