Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Transforming XML with XSLT and PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

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

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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · Hi, under php5 I got this down to the following:<?php$xsltProc = new...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek