Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

PHP and the Sablotron Processor
Pages: 1, 2, 3

Sounds pretty straightforward, right? In fact, it is. Of course, the first two steps of this process are out of the scope of the PHP script, since they relate to the Web server. Before moving on to step 3, I'll present the actual XML and XSL documents that are used in the news application. Listing 1-1 contains the XML document, titled news.xml, and listings 1-2 and 1-3 contain the XSL transformation documents for the PC browser (pcnews.xsl) and WAP browser (wapnews.xsl), respectively.

Listing 1-1: The news.xml document



<?xml version= "1.0">
<news xmlns:news="news.dtd">
<newsitem>
<title>PHP 4.04 is released!</title>
<submissionDate>December 19, 2000</submissionDate>
<author>PHP development team</author>
<story>To the chagrin of bewildered rivals, PHP 4.04 was released today.
Several bug fixes and enhancements to both the core and many extensions
are included in the release.</story>
</newsitem>
<newsitem>
<title>A Programmer's Introduction to PHP 4.0 is available!</title>
<submissionDate>January 5, 2001</submissionDate>
<author>W.J. Gilmore</author>
<story>In a shameless plug for himself, W.J. Gilmore announces the
official publishing date of his PHP textbook. Get your copy while it's
hot!</story>
</newsitem>
<newsitem>
<title>PHP: A benefit to the environment?</title>
<submissionDate>January 15, 2001</submissionDate>
<author>Greenpeace</author>
<story>In what's being hailed as one of the breakthrough discoveries of
the 21st century, scientists announced today that since PHP's inception
in 1995, the ozone hole has actually shrunk by 34 percent. Could there be a
direct correlation here?</story>
</newsitem>
</news>

Listing 1-2: The pcnews.xsl document

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="utf-8"/>

<xsl:template match="/ledger">
<html>
<head>
<title>What's new in programming?</title>
</head>
<body bgcolor="#ffffff" text="#000000" link="#808040"
vlink="#000000" alink="#606060">
<table bgcolor="white" border="0" cellpadding="2" cellspacing="2">
<xsl:call-template name="newsitems"/>
<table>
</body>
</html>
</xsl:template>

<xsl:template name="newsitems">
<xsl:for-each select="newsitem">
<tr><td>
<b><xsl:value-of select="title"/></b><br/>
Date: <xsl:value-of select="submissionDate"/><br/>
Submitted by: <xsl:value-of select="author"/><br/>
<xsl:value-of select="story"/><br/>
</td></tr>
</xsl:for-each>
<xsl:template>

</xsl:stylesheet>

Listing 1-3: The wapnews.xsl document

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="utf-8"/>

<xsl:template match="/ledger">
<wml>
<card id="card1" title="News">
<xsl:for-each select="newsitem">
<p>
<b><xsl:value-of select="title"/></b><br/>
<xsl:value-of select="submissionDate"/><br/>
<xsl:value-of select="author"/><br/>
<xsl:value-of select="story"/><br/>
</p>
</xsl:for-each>
</card>
</wml>
</xsl:template>
</xsl:stylesheet>

Listing 1-4 illustrates how the requesting browser type can be detected and how the determination of device type can be assumed as a result of this detection. This detection method is rather simple, since it only distinguishes those user agents sent by Nokia, Ericsson, and Phone.com (the UP.SDK browser) to make up the family of WAP browsers. However, you can easily add to this list as necessary. Furthermore, the function determines that anything not considered to be a WAP browser must be HTML, which is also not necessarily the case, since it discounts devices such as PDAs and pagers.

Listing 1-4: A simple browser/device detector (xmllib.inc)

function DeviceType() {

$browser_array = array("Noki", "Eric", "UP.B");
$browser = substr( trim ($HTTP_USER_AGENT), 0, 4);

if (in_array($browser, $browser_array)) :

$xsltFile = "wapnews.xsl";

else :

$xsltFile = "pcnews.xsl";

endif;

return $xsltFile;

} // end DeviceType()

Pages: 1, 2, 3

Next Pagearrow




-->