Tags:
create new tag
, view all tags

Recipe showing how to place all table fields in one set. This simplifies some types of query.

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"
  xmlns:set="http://exslt.org/sets"
  xmlns:exslt="http://exslt.org/common"
  extension-element-prefixes="set">

<!-- place each field in a set with attributes for table, title, row & column info --> 
<!-- The field title is taken as the value in the first row of this column -->

<xsl:variable name="fields_by_column">
    <!-- http://www-106.ibm.com/developerworks/xml/library/x-tipxsltmp.html -->
    <xsl:call-template name="find_fields_by_column"/>
</xsl:variable>

<!-- This template processes each field and tranforms it into a node /fields/field -->
<xsl:template name="find_fields_by_column">
    <xsl:param name="find_table"/>
    <fields>
    <!-- process each table -->
    <xsl:for-each select="/twiki/web/topic/data/tables/table">
        <!-- extract the table name and topic name -->
        <xsl:variable name="table" select="@name"/>
        <xsl:variable name="topic" select="../../@topic"/>

    <!-- process each row after the first header row -->
    <xsl:for-each select="row[position()>1]">
        <xsl:variable name="row" select="position()"/>
        <xsl:for-each select="field">
            <xsl:variable name="col" select="position()"/>
            <field>
                <xsl:attribute name="title">
                  <!-- extract the title from the field in the same column 1st row -->
                  <xsl:value-of select="../../row[1]/field[position() = $col]"/>
                </xsl:attribute>
                <xsl:attribute name="column">
                    <xsl:value-of select="$col"/>
                </xsl:attribute>
                <xsl:attribute name="row">
                    <xsl:value-of select="$row"/>
                </xsl:attribute>
                <xsl:attribute name="table">
                    <xsl:value-of select="$table"/>
                </xsl:attribute>
                <xsl:attribute name="topic">
                    <xsl:value-of select="$topic"/>
                </xsl:attribute>
                <xsl:value-of select="text()"/>
            </field>
        </xsl:for-each>
        </xsl:for-each>
    </xsl:for-each>
    </fields>
</xsl:template>



<xsl:template match="/">
<table>

<tr><th>Name</th><th>Value</th><th>Table</th><th>Title</th><th>Topic</th><th>Column</th><th>Row</th></tr>

<!-- use exslt:node-set to allow the parsing of the variable as a set of nodes -->
<xsl:for-each select="exslt:node-set($fields_by_column)//field">
     <tr>
          <td> <xsl:value-of select="name(.)"/> </td>
          <td> <xsl:value-of select="."/> </td>
          <td> <xsl:value-of select="@table"/> </td>
          <td> <xsl:value-of select="@title"/> </td>
          <td> <xsl:value-of select="@topic"/> </td>
          <td> <xsl:value-of select="@column"/> </td>
          <td> <xsl:value-of select="@row"/> </td>
     </tr>
</xsl:for-each>
</table>

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

%XSLTSTART{id="Plugins" benchmark="off" debug="off" }% <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:set="http://exslt.org/sets" xmlns:exslt="http://exslt.org/common" extension-element-prefixes="set">

<xsl:call-template name="find_fields_by_column"/>

<xsl:for-each select="/twiki/web/topic/data/tables/table">

<xsl:for-each select="row[position()>1]"> <xsl:for-each select="field"> <xsl:value-of select="../../row[1]/field[position() = $col]"/> <xsl:value-of select="$col"/> <xsl:value-of select="$row"/> <xsl:value-of select="$table"/> <xsl:value-of select="$topic"/> <xsl:value-of select="text()"/> </xsl:for-each> </xsl:for-each> </xsl:for-each>

<xsl:for-each select="exslt:node-set($fields_by_column)//field">

</xsl:for-each>
NameValueTableTitleTopicColumnRow
<xsl:value-of select="name(.)"/> <xsl:value-of select="."/> <xsl:value-of select="@table"/> <xsl:value-of select="@title"/> <xsl:value-of select="@topic"/> <xsl:value-of select="@column"/> <xsl:value-of select="@row"/>

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