↑
Main Page
XSLT support in Mozilla
The
setStartMode()
method accepts only one argument, which is the mode to set to. Just like
addParameter()
, this must be called before
transform()
.
If you are going to do multiple transformations using the same style sheet, you can reset the processor
after each transformation. When you call the
reset()
method, the input and output properties are
cleared and the processor is ready to be used again:
oProcessor.reset();
Because the processor has compiled the XSLT style sheet, it is faster to make repeat transformations ver-
sus using
transformNode()
.
XSLT support in Mozilla
Beginning in Mozilla 1.2, a new object called
XSLTProcessor
has been available to JavaScript developers
in order to enable client-side XSLT transformations. This object uses Mozilla’s built-in XSLT processor,
Transformiix, to enable this functionality.
The first step in the transformation is to load both the XML and XSLT into DOMs:
oXmlDom.load(“employees.xml”);
oXslDom.load(“employees.xslt”);
Then, create the
XSLTProcessor
and use the
importStylesheet()
method to assign the XSLT DOM:
var oProcessor = new XSLTProcessor()
oProcessor.importStylesheet(oXslDom);
The last step is to call either
transformToDocument()
or
transformToFragment()
with the XML
DOM as an argument to produce a result. As you may have guessed,
transformToDocument()
returns
a new DOM document as its result and
transformToFragment()
returns a new document fragment as
its result. Generally speaking, you should use
transformToDocument()
unless you intend to add the
result directly to an existing document; then you should use
transformToFragment()
.
When using
transformToDocument()
, just pass in the XML DOM and use the result as another com-
pletely different DOM:
var oResultDom = oProcessor.transformToDocument(oXmlDom);
alert(oResultDom.xml);
When using
transformToFragment()
, pass in the XML DOM as well as the document you intend to
add the result to. This ensures that the new document fragment is valid in the destination document:
MSXML only supports XSLT 1.0. Development on MSXML has stopped since the
movement to the .NET framework. It is expected that, at some point in the future,
JavaScript will have access to the XML and XSLT .NET objects.
477
XML in JavaScript
18_579088 ch15.qxd 3/28/05 11:42 AM Page 477
Free JavaScript Editor
Ajax Editor
©
→