You are here: Foswiki>System Web>XMLQueryPlugin>XmlQueryPluginRecipe4 (28 Feb 2006, PatrickDiamond)Edit Attach

Recipe showing how to restructure a table or set of tables

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

Annotated 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']">
Foreach table with the first field in the first row being a header with the text 'Title'
     <xsl:for-each select="../following-sibling::*">
Foreach row following the first one
         <table border="1">
         <tr><th>Key</th><th>Value</th></tr>
         <xsl:for-each select="field">
Foreach field tag within this row
             <xsl:variable name="current-position" select="position()"/>   
Note the position of the current field
             <tr>
                <td><xsl:value-of select="../../row[position()=1]/field[position()=$current-position]"/></td>
Extract the title of this field going to the first row then the field with the same position as the current field
                <td><xsl:value-of select="."/></td>
             </tr>
         </xsl:for-each>
     </table>
     </xsl:for-each>
  </xsl:for-each>

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

XSLT Demo: Table Restructured using XSLT

XmlQuery must be Installed for this demo to work. If %XSLTSTART{}% tag appears XmlQueryPugin is not installed %XSLTSTART{id="System" benchmark="off" debug="off" }% <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

KeyValue

%XSLTEND%
Topic revision: r5 - 28 Feb 2006, PatrickDiamond
This site is powered by FoswikiCopyright &© by the contributing authors. All material on this site is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback