%META:TOPICINFO{author="TWikiContributor" date="1302480031" format="1.1" version="$Rev$"}%
%META:TOPICPARENT{name="XmlQueryPlugin"}%
---+!! !XmlQueryPlugin Recipe 6: TWiki Syntax from XSLT

XmlQueryPlugin Recipe 6 shows how to output TWiki syntax from XSLT.

%TOC%

   * 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 <nop>XmlQueryPlugin must be called before that plugin. In practise this means setting <nop>XmlQueryPlugin near the start of the plugin list described by the __INSTALLEDPLUGINS__ setting in Main.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

<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 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%
</verbatim>

---++ XSLT Demo Transformed Table
*XmlQuery must be Installed for this demo to work. The <nop>XmlQueryPugin is not installed properly if the %<nop>XSLTSTART{}% variable appears below*

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="/">

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

-----
__Related:__ %SEARCH{ "XmlQueryPlugin" scope="topic" topic="XmlQueryPlugin*" excludetopic="%TOPIC%" nonoise="on" format="$topic" separator=", " }%
