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.
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.