Home arrow PHP arrow Page 2 - More PHP, XML and XSL for Wireless Content
PHP

More PHP, XML and XSL for Wireless Content


In this article, Jason will demonstrate how to use PHP with XSL to manage your XML data in numerous wireless technologies.

Author Info:
By: Jason Lam
Rating: 5 stars5 stars5 stars5 stars5 stars / 32
April 29, 2003
TABLE OF CONTENTS:
  1. · More PHP, XML and XSL for Wireless Content
  2. · The Article
  3. · Summary

print this article
SEARCH DEVARTICLES

More PHP, XML and XSL for Wireless Content - The Article
(Page 2 of 3 )

The Familiar HTML

Okay, let us start with the familiar HTML example first. As well present the PHP code used to transform the stylesheet and the XML data itself.

PHP Source Code:

<?php
include_once("./xslt.class.php");
$xslt = new Xslt("c://data//books_html.xsl", "c://data//books.xml");
header("Content-type: text/html");
print ($xslt->transform());
?>

PHP Helper Class Source Code:

class Xslt {
  var $xsl_file;
  var $xml_file;
  var $fileName;
  function Xslt($xsl_file = '', $xml_file = '') {
    $this->xsl_string = "file://" . $xsl_file;
    $this->xml_string = "file://" . $xml_file;    
  }
  function transform() {
    $this->result = '';
    $xsltHandle = xslt_create(); 
    //xslt_set_base($xsltHandle,'file://c://php//xslxml//');  
    $this->result = xslt_process($xsltHandle,$this->xml_string,$this->xsl_string);
    //$this->result = xslt_process($xsltHandle,'file://d://' . $this->xml_string,$this->xsl_string);
    if (!$this->result)
      die("Transformation failed");
    xslt_free($xsltHandle);
    return $this->result;
  }
}

XML File

<?xml version="1.0" encoding="UTF-8"?>
<books>
  <book>
    <title>Professional PHP4 Programming </title>
    <author>Deepak Thomas</author>
    <price>49.99</price>
    <special>34.99</special>
    <size>Paperback 974 pages ; Dimensions (in inches): 2.08 x 9.00 x 7.26</size>
    <publisher>Wrox Press Inc; 1st edition (January 2002)</publisher>
    <isbn>1861006918</isbn>
  </book> 
  <book>
    <title>PHP Fast and Easy Web Development, 2nd Edition</title>
    <author>Julie C. Meloni</author>
    <price>29.99</price>
    <special>0.00</special>
    <size>Paperback 504 pages ; Dimensions (in inches): 1.03 x 9.24 x 7.32</size>
    <publisher>Premier Press; 2 edition (August 26, 2002)</publisher>
    <isbn>193184187X</isbn>   
  </book> 
  <book>
    <title>PHP MySQL Website Programming: Problem - Design - Solution </title>
    <author>Chris Lea</author>
    <price>34.99</price>
    <special>24.99</special>
    <size>Hard Cover 500 pages ; Dimensions (in inches): 1.15 x 9.08 x 7.22</size>
    <publisher>Wrox Press Inc; (March 2003)</publisher>
    <isbn>1861008279</isbn>   
  </book>   
</books>

XSL File

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
  <html>
  <body>
    <h2>PHP Books</h2>
    <table border="0" cellspacing="0" cellpadding="0">
      <xsl:for-each select="books/book">
      <tr>
        <td>
          <u><b><xsl:value-of select="title"/></b></u><br/>
          <b><xsl:text>Author: </xsl:text></b><xsl:value-of select="author"/><br/>
          <b><xsl:text>Price: </xsl:text></b><xsl:value-of select="price"/> 
          <xsl:if test="special > 0">
            <b><xsl:text> On Sale For: </xsl:text><xsl:value-of select="special"/></b>
          </xsl:if><br/>
          <b><xsl:text>Paperback: </xsl:text></b><xsl:value-of select="size"/><br/>
          <b><xsl:text>Publisher: </xsl:text></b><xsl:value-of select="publisher"/><br/>
          <b><xsl:text>ISBN: </xsl:text></b><xsl:value-of select="isbn"/><br/>         
          <br/>                         
        </td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Output:

The XML file is your key file where dynamic data is stored, changed or removed. It does not affect any the layout of the pages themselves.  This you should already know.  After looking at the above code and from your experience working with XSL to transform XML files to HTML output, you are probably saying to yourself this is not worth it. 

I agree with you one hundred percent, not only is it a lot of work; but also requires you add to your skill set XSL and XML.  In fact, if there is no requirement to output the data to anything but a regular browser it might be in your best interest to simply stick to PHP and HTML.  But if you are going to support other devices or media you should seriously consider using PHP and XSL. 

The following three XSL transformations will demonstrate this, the technologies used will be WML, cHTML and VXML,

WML

PHP Source Code:

<?php
include_once("./xslt.class.php");
$xslt = new Xslt("c://data//books_wml.xsl", "c://data//books.xml");
header("Content-type: text/vnd.wap.wml");
print ($xslt->transform());
?>

XSL File

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format
">
<xsl:template match="/"> 
    <wml>      
       <template>
         <do type="accept" label="Back"><prev/></do>
       </template>
       <xsl:element name="card">
         <xsl:attribute name="id">books</xsl:attribute>
         <xsl:attribute name="books">PHP Books</xsl:attribute>                
          <p>
             <small>           
               <xsl:for-each select="books/book">
                 <u><b><xsl:value-of select="title"/></b></u><br/>
                 <b><xsl:text>Author: </xsl:text></b><xsl:value-of select="author"/><br/>
                 <b><xsl:text>Price: </xsl:text></b><xsl:value-of select="price"/> 
                 <xsl:if test="special > 0">
                   <b><xsl:text> On Sale For: </xsl:text><xsl:value-of select="special"/></b>
                 </xsl:if><br/>
                 <b><xsl:text>Paperback: </xsl:text></b><xsl:value-of select="size"/><br/>
                 <b><xsl:text>Publisher: </xsl:text></b><xsl:value-of select="publisher"/><br/>
                 <b><xsl:text>ISBN: </xsl:text></b><xsl:value-of select="isbn"/><br/>                    
                 <br/>               
               </xsl:for-each>
             </small>
          </p>
       </xsl:element>            
    </wml>
</xsl:template>

Output:

cHTML

PHP Source Code:

<?php
include_once("./xslt.class.php");
$xslt = new Xslt("c://data//books_chtml.xsl", "c://data//books.xml");
header("Content-type: text/html");
print ($xslt->transform());
?>

XSL File

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format
">
<xsl:template match="/"> 
    <html>
      <head>     
         <title><xsl:value-of select="page/title"/></title>
      </head>
      <body>
      <xsl:for-each select="books/book">
        <small>
          <u><b><xsl:value-of select="title"/></b></u><br/>
          <b><xsl:text>Author: </xsl:text></b><xsl:value-of select="author"/><br/>
          <b><xsl:text>Price: </xsl:text></b><xsl:value-of select="price"/> 
          <xsl:if test="special > 0">
            <b><xsl:text> On Sale For: </xsl:text><xsl:value-of select="special"/></b>
          </xsl:if><br/>
          <b><xsl:text>Paperback: </xsl:text></b><xsl:value-of select="size"/><br/>
          <b><xsl:text>Publisher: </xsl:text></b><xsl:value-of select="publisher"/><br/>
          <b><xsl:text>ISBN: </xsl:text></b><xsl:value-of select="isbn"/><br/>                    
          <br/>
        </small>
      </xsl:for-each> 
      </body>
    </html>
</xsl:template>
</xsl:stylesheet>

Output:

VXML

PHP Source Code:

<?php
include_once("./xslt.class.php");
$xslt = new Xslt("c://data//books_vxml.xsl", "c://data//books.xml");
header("Content-type: text/xml");
print ($xslt->transform());
?>

XSL File

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/"> 
     <vxml version="2.0">    
       <form id="intro">
         <block>
         <xsl:for-each select="books/book">        
          <xsl:value-of select="title"/><break size="large"/><break size="large"/>
          <xsl:text>Author </xsl:text><xsl:value-of select="author"/><break size="large"/>
          <xsl:text>Price </xsl:text><xsl:value-of select="price"/><break size="large"/>
          <xsl:if test="special > 0">
            <xsl:text>On Sale For </xsl:text><xsl:value-of select="special"/><break size="large"/>
          </xsl:if>
          <xsl:text>Paperback: </xsl:text><xsl:value-of select="size"/><break size="large"/>
          <xsl:text>Publisher: </xsl:text><xsl:value-of select="publisher"/><break size="large"/>
          <xsl:text>ISBN: </xsl:text><xsl:value-of select="isbn"/><break size="large"/>
           </xsl:for-each>
         </block>        
       </form>     
     </vxml>
</xsl:template>
</xsl:stylesheet>

Output:

It is unlikely you will have the voice-recognition technology equipment to run the VXML example.  However, you can visit one of the VoiceXML providers such as VoiceGenie.com or Tellme.com.  As well in this demonstration the output will stream the entire information from start to finish.  When developing VoiceXML applications you should tailor the application towards voice interaction. 

In this example, it would probably be better to list out the titles of the books in menu like fashion, possible as abbreviated titles.  From there the user should select an option and thereafter the details of the book would be told to the user.  The implementation of this is beyond the scope of this article.

The Benefits

Because we are using the same content in all cases, whether it is HTML, WML, cHTML or VXML the same XML data file is used.  If additional books are added, books need to be removed or changes need to be made such as sales price all you really have to do is change the data in the XML file.  As well this provides another layer of abstraction between the presentation layer and server-side code. 

It keeps your code a lot cleaner rather then having several different files or several if statements in your PHP code to output the appropriate content for the appropriate viewer.  As well it prevents you from inserting difficult to find and difficult to debug PHP code in all the different files for all the different devices being supported.

However, you will notice the Sale price is only displayed based upon the if statement in the XSL stylesheet, if this for some reason needed to be changed you would have to make the change in each XSL stylesheet.  To avoid this, simply generate dynamic XML files with PHP and have your logic in the PHP code determine what goes into the XML file. 

As well you can then use PHP to determine which appropriate pre-defined XSL stylesheet to use.  This way your XML files are purely used as means of holding data in meaningful structure without any processing logic at all.

Furthermore, you have not only saved maintenance time but as well reduced the amount of effort needed if you were to support other features such as:

 Reports in  PDF format
 RSS feeds
 WebServices
 SVG
 ChessML
 AvantGo
 AIML
 XHTML
 HDML

the list goes on and on.

Web Services is especially important when you want to start sharing the same information to other businesses or simply to other devices that are not script based such as SymbianOS devices, Palm, J2ME enabled devices and SmartPhones.   This is because you are working with an agreed upon industry standard largely based on XML.


blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials