Part 1 of the XSLT presentation I gave at WSG

Sample XSLT Transformation

XSLT uses 'template rules' to transform the original XML data into HTML. Shown below is a short example of how the transformation process works.

A book entry with 2 authors

Four our simplified example we will use XML data that describes a book entry with a title, two authors and a message about one of the authors.
<book title='Winemakers Guide to Success' >
<author>Peter Bovine</author>
<message>Peter will be instore this Friday</message>
<author>Wilfred Cunningham</author>
</book>
Look closely at the tags and attributes in the XML above. They describe the content in a clear and concise manner.

The XSLT

XSLT uses 'template rules' to produce the HTML. In this example there are 3 rules: one for the book, one for the author and one for the message.
<!--
Template rule - book
Description - Provides formatting for the book elements.
-->

<xsl:template  match='book' >
<div  class='book' >
<span  class='title' ><xsl:value-of select='@title' /></span>
<xsl:apply-templates /> <!-- finds any matching template rules-->
</div>
</xsl:template>

<!--
Template rule - author
Description - Provides formatting for the author elements.
-->

<xsl:template  match='author' >
Author: <xsl:value-of select="."/> <br/>
</xsl:template>

<!--
Template rule - message
Description - Provides formatting for the message elements.
-->

<xsl:template  match='message' >
<strong><xsl:value-of select="."/></strong> <br/>
</xsl:template>

The HTML that will be produced

And the result would be the followng HTML. It has the book title, an author, a message and the second author as it appears in the XML data, but with the layout code supplied by the matching template rules.
<div  class='book' >
<span  class='title' >Winemakers Guide to Success</span>
Author: Peter Bovine <br/>
<strong>Peter will be instore this Friday</strong> <br/>
Author: Wilfed Cunningham <br/>
</div>
Next... XSLT Candidates