Transforming XML with XSLT and PHP - The XSLT Template
(Page 3 of 5 )
Now, let's take a look at the XSLT template:
<xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- ########################################################## XSL Transformation to create a table of cd information (title, artist, genre, track numbers and track listings) from an XML document portraying a cd collection. ########################################################### -->
<xsl:template match="/"> <!-- ########################################################## This template matches the "root" element, catalog. It inserts <html> and </html> tags and sets up <head> and <body> sections. In the <body> it sets up the framework and header row of a table in which to display the XML document's data. ########################################################### --> <html> <head> <title>My CD Collection</title> </head> <body> <table> <tr><th colspan="2"> <h1>My CD Collection</h1> </th></tr> <!-- apply the other templates here --> <xsl:apply-templates /> </table> </body> </html> </xsl:template>
<xsl:template match="cd"> <!-- ########################################################## This is the template to apply when a cd element is encountered in transforming the XML document. It sets up a row of the data table previously started (and that will be ended) by the root element's template. ########################################################### --> <tr><td valign="top"> <!-- display attribute values for title, artist, genre --> <p><b>Title: </b> <xsl:value-of select="@title" /> <br /> <b>Artist: </b> <xsl:value-of select="@artist" /> <br /> <b>Genre: </b> <xsl:value-of select="@genre" /> <br /> </p> </td><td valign="top"> <!-- apply the track template here --> <p><xsl:apply-templates select="track" /></p> </td></tr> </xsl:template>
<xsl:template match="track"> <!-- ########################################################## This is the template for the track element. ########################################################### --> <!-- select and display track ID --> <xsl:value-of select="@id" /> <!-- display text in element --> <i><xsl:value-of select="." /></i><br /> </xsl:template>
</xsl:stylesheet> |
The XSLT is rather well documented and could be considered rather self-explanatory. However, there are still a few ideas and lines worth mentioning. First, note that the entire XSLT is nested within the xsl:stylesheet element.
There are a series of template rules nested within the xsl:stylesheet element. The xsl:template's match attribute specifies the matching element in the XML document to which it should apply the template. The XML document will be scanned and the templates will be applied accordingly.
When processed, XSLT applies the template matching "/" to the catalog element of the XML document because that is the root element. The template will insert the information within the template up until the xsl:apply-templates element, at which point it will look for other template matches. After other template matches have been processed it will insert the remaining information and conclude the document.
The other templates work the same way. They match the elements cd and track and insert their nested contents.
The xsl:value-of element inserts the information found in the XML document that matches the value set by the select attribute. The element name can be used with the select attribute to instruct XSLT to insert the data identified within the element's tags. A dot matches all of the current text. Select values beginning with a "@" displays the value of the corresponding attribute of the matched element.
After all of the XSLT instructions have all been processed, the templates have been combined to create an HTML document displaying the information contained within the XML document.
Next: Transforming the Document >>
More Miscellaneous Articles
More By bluephoenix