Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Transforming XML with XSLT and PHP
MISCELLANEOUS

Transforming XML with XSLT and PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 12
    2003-10-12

    Table of Contents:
  • Transforming XML with XSLT and PHP
  • The XML Document
  • The XSLT Template
  • Transforming the Document
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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.

    More Miscellaneous Articles
    More By bluephoenix

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap