Friday, November 8, 2024

PHP, XML and XSL for Wireless Content

This article is another illustration of why using PHP with XSL to transform XML data to various presentation layers is beneficial. With that being said this article will demonstrate how to present the same data to several different wireless technologies using PHP and XSL, instead of an article completely focused on PHP and XSL.

There are plenty of articles on the web that already do this, but most them only focus on presenting data through HTML. This is what this article is for to give more examples of XSL other then with HTML. A recent article on “Using PHP and XSL to Transform XML into Web Content” by Benson Wong is available at Zend.com.

As well an installation on how to do a “Step by Step Install of Apache PHP SSL and more”, also includes XSL module, is available at
http://www3.telus.net/jasonlam604/linux002.html. As well you can refer to
http://www.xslt.com or
http://www.w3schools.com/default.asp for more information on XSL.

Brief Recap of XSL and XML

XSL (eXtensible Stylesheet Language) is used to transform XML into data/form for a particular device whether it be a screen, voice or paper so that it can understand what the XML data is. Reason being XML tags are not predefined so devices like browsers are unable to understand XML and that is what XSL is for. At the same time it allows for different stylesheets to be applied to the same XML data for different devices as the following examples will show.

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

$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>
&nbsp&nbsp <book>
&nbsp&nbsp&nbsp&nbsp <title>Professional PHP4 Programming </title>
&nbsp&nbsp&nbsp&nbsp <author>Deepak Thomas </author>
&nbsp&nbsp&nbsp&nbsp <price> 49.99 </price>
&nbsp&nbsp&nbsp&nbsp <special> 34.99 </special>
&nbsp&nbsp&nbsp&nbsp <size> Paperback 974 pages ; Dimensions (in inches): 2.08 x 9.00 x 7.26 </size>
&nbsp&nbsp&nbsp&nbsp <publisher> Wrox Press Inc; 1st edition (January 2002) </publisher>
&nbsp&nbsp&nbsp&nbsp <isbn> 1861006918 </isbn>
&nbsp&nbsp < /book>
&nbsp&nbsp <book>
&nbsp&nbsp&nbsp&nbsp <title> PHP Fast and Easy Web Development, 2nd Edition </title>
&nbsp&nbsp&nbsp&nbsp <author> Julie C. Meloni </author>
&nbsp&nbsp&nbsp&nbsp <price> 29.99 </price>
&nbsp&nbsp&nbsp&nbsp <special> 0.00 </special>
&nbsp&nbsp&nbsp&nbsp <size> Paperback 504 pages ; Dimensions (in inches): 1.03 x 9.24 x 7.32 </size>
&nbsp&nbsp&nbsp&nbsp <publisher> Premier Press; 2 edition (August 26, 2002) </publisher>
&nbsp&nbsp&nbsp&nbsp <isbn> 193184187X </isbn>
&nbsp&nbsp </book >
&nbsp&nbsp <book>
&nbsp&nbsp&nbsp&nbsp <title >PHP MySQL Website Programming: Problem - Design - Solution </title>
&nbsp&nbsp&nbsp&nbsp <author >Chris Lea </author>
&nbsp&nbsp&nbsp&nbsp <price> 34.99 </price>
&nbsp&nbsp&nbsp&nbsp <special >24.99 </special>
&nbsp&nbsp&nbsp&nbsp <size> Hard Cover 500 pages ; Dimensions (in inches): 1.15 x 9.08 x 7.22 </size>
&nbsp&nbsp&nbsp&nbsp <publisher> Wrox Press Inc; (March 2003) </publisher>
&nbsp&nbsp&nbsp&nbsp <isbn> 1861008279 </isbn>
&nbsp&nbsp </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="/">
&nbsp &nbsp <html>
&nbsp &nbsp <body>
&nbsp &nbsp&nbsp &nbsp <h2>PHP Books </h2>
&nbsp &nbsp&nbsp &nbsp < table border="0" cellspacing="0" cellpadding="0">
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < xsl:for-each select="books/book">
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < tr>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < td>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < u> < b> < xsl:value-of select="title"/> < /b> < /u> < br/>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < b> < xsl:text> Author: < /xsl:text> < /b> < xsl:value-of select="author"/> < br/>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < b> < xsl:text> Price: < /xsl:text> < /b> < xsl:value-of select="price"/>
< xsl:if test="special > 0">
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < b> < xsl:text> On Sale For: < /xsl:text> < xsl:value-of select="special"/> < /b>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < /xsl:if> < br/>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < 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/>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < br/>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < /td>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < /tr>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < /xsl:for-each>
&nbsp &nbsp&nbsp &nbsp&nbsp &nbsp < /table>
&nbsp &nbsp&nbsp &nbsp < /body>
&nbsp &nbsp< /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

$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="/">
&nbsp < wml>
&nbsp&nbsp < template>
&nbsp &nbsp&nbsp < do type="accept" label="Back"> < prev/> < /do>
&nbsp &nbsp < /template>
&nbsp&nbsp < xsl:element name="card">
&nbsp&nbsp&nbsp < xsl:attribute name="id"> books< /xsl:attribute>
&nbsp&nbsp&nbsp < xsl:attribute name="books"> PHP Books< /xsl:attribute>
&nbsp&nbsp&nbsp < p>
&nbsp&nbsp&nbsp&nbsp < small>
&nbsp&nbsp&nbsp&nbsp&nbsp < xsl:for-each select="books/book">
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < u> < b> < xsl:value-of select="title"/> < /b> < /u> < br/>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < b> < xsl:text> Author: < /xsl:text> < /b> < xsl:value-of select="author"/> < br/>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < b> < xsl:text> Price: < /xsl:text> < /b> < xsl:value-of select="price"/>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < xsl:if test="special > 0">
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < b> < xsl:text> On Sale For: < /xsl:text> < xsl:value-of select="special"/> < /b>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < /xsl:if> < br/>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < b> < xsl:text> Paperback: < /xsl:text> < /b> < xsl:value-of select="size"/> < br/>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < b> < xsl:text> Publisher: < /xsl:text> < /b> < xsl:value-of select="publisher"/> < br/>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < b> < xsl:text> ISBN: < /xsl:text> < /b> < xsl:value-of select="isbn"/> < br/>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp < br/>
&nbsp&nbsp&nbsp&nbsp&nbsp < /xsl:for-each>
&nbsp&nbsp&nbsp&nbsp < /small>
&nbsp&nbsp&nbsp < /p>
&nbsp&nbsp < /xsl:element>
&nbsp < /wml>
< /xsl:template>

Output:

cHTML

PHP Source Code:

  <?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="/">
&nbsp < html>
&nbsp &nbsp < head>
&nbsp &nbsp &nbsp < title>< xsl:value-of select="page/title"/>< /title>
&nbsp &nbsp < /head>
&nbsp &nbsp < body>
&nbsp &nbsp < xsl:for-each select="books/book">
&nbsp &nbsp &nbsp < small>
&nbsp &nbsp &nbsp &nbsp < u>< b>< xsl:value-of select="title"/>< /b>< /u>< br/>
&nbsp &nbsp &nbsp &nbsp < b>< xsl:text>Author: < /xsl:text>< /b>< xsl:value-of select="author"/>< br/>
&nbsp &nbsp &nbsp &nbsp < b>< xsl:text>Price: < /xsl:text>< /b>< xsl:value-of select="price"/>
&nbsp &nbsp &nbsp &nbsp < xsl:if test="special > 0">
&nbsp &nbsp &nbsp &nbsp &nbsp < b>< xsl:text> On Sale For: < /xsl:text>< xsl:value-of select="special"/>< /b>
&nbsp &nbsp &nbsp &nbsp < /xsl:if>< br/>
&nbsp &nbsp &nbsp &nbsp < b>< xsl:text>Paperback: < /xsl:text>< /b>< xsl:value-of select="size"/>< br/>
&nbsp &nbsp &nbsp &nbsp < b>< xsl:text>Publisher: < /xsl:text>< /b>< xsl:value-of select="publisher"/>< br/>
&nbsp &nbsp &nbsp &nbsp < b>< xsl:text>ISBN: < /xsl:text>< /b>< xsl:value-of select="isbn"/>< br/>
&nbsp &nbsp &nbsp &nbsp < br/>
&nbsp &nbsp &nbsp < /small>
&nbsp &nbsp < /xsl:for-each>
&nbsp &nbsp < /body>
&nbsp < /html>
< /xsl:template>
< /xsl:stylesheet>

Output:

VXML

PHP Source Code:

 <?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="/">
&nbsp <vxml version="2.0">
&nbsp &nbsp <form id="intro">
&nbsp &nbsp &nbsp <block>
&nbsp &nbsp &nbsp &nbsp <xsl:for-each select="books/book">
&nbsp &nbsp &nbsp &nbsp <xsl:value-of select="title"/><break size="large"/><break size="large"/>
&nbsp &nbsp &nbsp &nbsp <xsl:text>Author </xsl:text><xsl:value-of select="author"/><break size="large"/>
&nbsp &nbsp &nbsp &nbsp <xsl:text>Price </xsl:text><xsl:value-of select="price"/><break size="large"/>
&nbsp &nbsp &nbsp &nbsp <xsl:if test="special > 0">
&nbsp &nbsp &nbsp &nbsp &nbsp <xsl:text>On Sale For </xsl:text><xsl:value-of select="special"/><break size="large"/>
&nbsp &nbsp &nbsp &nbsp </xsl:if>
&nbsp &nbsp &nbsp &nbsp <xsl:text>Paperback: </xsl:text><xsl:value-of select="size"/><break size="large"/>
&nbsp &nbsp &nbsp &nbsp <xsl:text>Publisher: </xsl:text><xsl:value-of select="publisher"/><break size="large"/>
&nbsp &nbsp &nbsp &nbsp <xsl:text>ISBN: </xsl:text><xsl:value-of select="isbn"/><break size="large"/>
&nbsp &nbsp &nbsp &nbsp </xsl:for-each>
&nbsp &nbsp &nbsp </block>
&nbsp &nbsp </form>
&nbsp </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.

Summary

Even though there were several different types of technologies being used the over all goal was to demonstrate XSL is indeed beneficial when providing the same content to several different types of devices. As well if you aren’t familiar with some of the above wireless technologies that this article has now peeked your interests into something new.

Jason is a wireless and open source developer enthusiast who enjoys creating synergy and sharing knowledge in the software development world. To learn more about him visit his personal site at http://www.jasonlam604.com/

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

0FansLike
0FollowersFollow
0SubscribersSubscribe
- Advertisement -spot_img

Latest Articles

Warehouse.