↑
Main Page
XSLT parameters
var oResultFragment = oProcessor.transformToDocument(oXmlDom, document);
var oDiv = document.getElementById(“divResult”);
oDiv.appendChild(oResultFragment);
In the previous example, the processor creates a fragment owned by the
document
object. This enables
the fragment to be added to a
<div/>
element existing in the page.
This all makes perfect sense when the output method for XSLT is either HTML or XML, but what about
when the output is text? To solve this problem, Mozilla creates an XML document with a single element,
<transformiix:result/>
, that contains all the text output. So, using text output from an XSLT file still
results in a valid document or document fragment.
Keeping this in mind, it’s possible to create a
transformNode()
method for Mozilla:
Node.prototype.transformNode = function (oXslDom) {
var oProcessor = new XSLTProcessor();
oProcessor.importStylesheet(oXslDom);
var oResultDom = oProcessor.transformToDocument(this);
var sResult = oResultDom.xml;
if (sResult.indexOf(“<transformiix:result”) > -1) {
sResult = sResult.substring(sResult.indexOf(“>”) + 1,
sResult.lastIndexOf(“<”));
}
return sResult;
};
This method creates a result document using the given XSLT DOM. The resulting XML code is then stored
in
sResult
using the
xml
property defined earlier in the chapter. That code is then checked to see if it
contains
<transformiix:result/>
. If it does, then the XML part is stripped out (by taking only the
string between the first greater-than symbol and the last less-than symbol). Lastly,
sResult
is returned.
Using this method, you can create code to run in both Mozilla and IE:
var oXmlDom = new XmlDom();
var oXslDom = new XmlDom();
oXmlDom.async = false;
oXslDom.async = false;
oXmlDom.load(“employees.xml”);
oXslDom.load(“employees.xslt”);
alert(oXmlDom.transformNode(oXslDom));
The
XSLTProcessor
in Mozilla also allows you to set XSLT parameters. The
setParameter()
method
accepts three arguments: the namespace URI, the parameter local name, and the value to set. Typically,
the namespace URI is
null
and the local name is simply the parameter ’s name. This method must be
called prior to
transformToDocument()
or
transformToFragment()
:
478
Chapter 15
18_579088 ch15.qxd 3/28/05 11:42 AM Page 478
Free JavaScript Editor
Ajax Editor
©
→