Tags:
create new tag
, view all tags

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="Plugins" benchmark="off" debug="off" }% <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<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::*">

<xsl:for-each select="field"> </xsl:for-each>
KeyValue
<xsl:value-of select="../../row[position()=1]/field[position()=$current-position]"/> <xsl:value-of select="."/>
</xsl:for-each> </xsl:for-each>

%XSLTEND%

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2006-02-13 - PatrickDiamond
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.