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
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.
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 arent familiar with some of the above wireless technologies that
this article has now peeked your interests into something new.
|