|
Using XML: A PHP Developer's Primer, Part 3
Adam Delves
Server Side PHP
The PHP script single_book.php, is the server side portion of the Ajax
application. It receives two variables in the query string: the isbn
number and a boolean variable indicating whether the browser sending
the request supports XSLT. The script either outputs XML or HTML
depending upon the value of the xslt variable.
PHP
- single_book.php:
<?php
if (@$_GET['source']) {
highlight_file(__FILE__);
exit;
}
/* load external variables */
$isbn = htmlentities(@$_GET['isbn']);
$xslt = (boolean) @$_GET['xslt'];
/* this DOMDocument will contain the XML for a single book */
$returnDom = new DOMDocument;
/* load the complete libarary XML */
$xml = new DOMDocument('1.0');
$xml->load('xml/library.xml');
/* use an xPath expression to extract the book containing the isbn number we want */
$xPath = new DOMXpath($xml);
$book = $xPath->evaluate("/library/books/book[@isbn='$isbn']")->item(0);
/* if the book was found, append it to our return DOM object */
if ($book) {
$book = $returnDom->importNode($book, true);
$returnDom->appendChild($book);
}
if ($xslt) { // browser supports XSLT, let it do the transformation
header('Content-Type: application/xml'); // send the correct MIME type
echo($returnDom->saveXml());
} else {// borwser does not support XSLT, therefore we do the transformation on its behalf
/* load the stylesheet */
$xsl = new DOMDocument;
$xsl->load('xml/single_book_html.xsl');
/* import the stylesheet */
$processor = new XSLTProcessor;
$processor->importStyleSheet($xsl);
/* transform it to HTML and output the result */
echo($processor->transformToXml($returnDom));
}
?>
Javascript Helper Functions
Due to the different implementations of XSLT, the Ajax engine is the
most complex part of the application. Several helper
functions
have been defined, which are as follows. Each function chooses the
browser-dependent method to complete the task.
- createDomFromUri(uri,
freeThreaded)
– loads and returns an instance of a DOMDocument from a
specified
location on the Internet. n.b: only documents from the same domain can
be loaded
freeThreaded - (Internet Explorer Only) If set to true a FreeThreadedDomDocument is returned.
- getDom(freeThreaded)
– returns an instance of DOMDocument object.
freeThreaded - (Internet Explorer Only) If set to true a FreeThreadedDomDocument is returned.
- getXMLHTTP()
- returns an instance of an XMLHTTPRequest object. Returns false if it
is unavailable.
- loadXmlFromString(sXml)
- loads an XML DOMDocument object from a valid string
representation of XML.
sXML - the string representation of the XML to be parsed
- loadXslStylesheet(uri)
– loads an XSLT stylesheet returning an
XSLTProcessor object
uri – the location of the XSLT stylesheet - parseXmlDom(oXmlDom)
– returns true if the Xml document is valid and well formed
and false if not
oXmlDom – the DOMDocument to check
- transformXml(oXmlDom,
oXsltProcessor)
– transforms an XML DOMDocument. W3c compliant browsers
return a
DOMDocument object representing the transformed XML. Internet Explorer
returns a string representation of the transformed XML.
oXmlDom – the XML DOMDocument object to be transformed
oXsltProcessor – the XSLT processor object to use when making the transformation.
The code from these functions is declared in the
xml_helper_functions.js file contained in the ZIP file accompanying this article.
| Comments: | ||
No Messages FoundYou can post questions/corrections/feedback here
| ||
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


