![]() Join Up! 96815 members and counting! |
|
|||
Using XML: A PHP Developer's Primer, Part 3
Adam Delves
Internet Explorer
In Internet Explorer, the DOMElement object supports the transformNode
method. However, each time this method is called,
the XSL stylesheet must be recompiled, making it very sluggish.
Microsoft does however provide the MSXML2.XslTemplate Active X control which is similar to the XSLTProcessor. The compiled stylesheet is cached by the browser, therefore transformations are a lot quicker. Like the XSLTProcessor object in Firefox and PHP, the stylesheet is imported as DomDocument object. The XSLTemplate object requires a FreeThreadedDomDocument, which implements an identical interface to the standard DomDocument object, with the difference of allowing simultaneous access to the object from multiple threads. var oXsl = new
ActiveXObject('Msxml2.FreeThreadedDOMDocument.3.0');If an error occurred while parsing the XML, the parseError, property of the DOMDocument will be available and the errorCode will be non zero: if ((oXsl.parseError)
&&
(oXsl.parseError.errorCode != 0))
{ We now need to load the style sheet into the processor and we are ready to make the transformation: var oXslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");
An XSLT and Ajax Example
This is where the technologies of XML, XSLT and xPath all come
together. Through the correct utilisation of these technologies, where
available, you can reward your visitors with a feature-rich user
interface.
Extending our original XSLT application by adding an Ajax module, we will display the picture and the book synopsis when the users mouse pointer hovers over a link to a specific book. If the web browser supports XSLT, the PHP script will return the XML for the book requested and allow the browser to transform it. If not, the XML is transformed by the PHP script and returned as HTML.
The XSL Stylesheet
We first start by making a small change to the book_summary.xsl
stylesheet, adding a small CSS stylesheet and a link to the external
Javascript containing the Ajax engine to the root template.
XSL: <xsl:template match="/library">
We also need an additional XSL
stylesheet which will transform the XML
for a single book, to HTML.
XSL
- single_book_html.xsl:<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|