Tags:
create new tag
, view all tags

Recipe showing how to output TWiki syntax from XSLT

  • This is the same Recipe as XmlQueryPluginRecipe3 except that the output table is in TWiki syntax. This allows the TablePlugin to process the output enabling sortable columns etc
  • A cavate with this techinique is that if the TWiki tag being output by the XSLT is available via a Plugin then XmlQueryPlugin must be called before that plugin. In practise this means setting XmlQueryPlugin near the start of the plugin list described by the INSTALLEDPLUGINS setting in WebPreferences .

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

%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 style Table header -->
<xsl:text>%TABLE{name="test"}%
| *Title* | *Author* |
</xsl:text>

<!-- Loop Over each Row > 1 in tables which match field at row 1 col 1 type is title-->
 <xsl:apply-templates select="/twiki/web/topic/data/tables/table/row[position()=1]/field[@type='title' and position()=1 and text()='Author']/../../row">
     <!-- Sort by the contents of the second field -->
     <xsl:sort select="field[2]"/>
 </xsl:apply-templates>

<!-- Add a newline at the end -->
<xsl:text>
</xsl:text>
</xsl:template>


<xsl:template match="row">
    <!-- Process each line where at least the first field is of type data -->
    <xsl:if test="./field[1][@type='data']">
       <xsl:text>| </xsl:text>         
       <xsl:value-of select="./field[2]"/>
       <xsl:text> | </xsl:text>
       <xsl:value-of select="./field[1]"/>
       <!-- add a new line at the end -->
       <xsl:text> |
</xsl:text>
    </xsl:if>

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

XSLT Demo Transformed Table

XmlQuery must be Installed for this demo to work. If %XSLTSTART{}% tag appears XmlQueryPugin is not installed

The above tables has been transformed in 3 ways

  1. They have been combined
  2. The Author and Title columns have been swapped
  3. The entries have been sorted by Title

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

Title Author

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

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

%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.