We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

Using the Mozilla JavaScript interface to XSL Transformations


This document describes the JavaScript interface in Mozilla 1.2 and up to the XSLT Processing Engine (TransforMiiX).

Creating an XSLTProcessor

To start, you need to create an XSLTProcessor object:

var processor = new XSLTProcessor();

Specifying the stylesheet

Before you can use it, you must import a stylesheet with the importStylesheet() function. It has a single parameter, which is the DOM Node of the XSLT stylesheet to import - note that the import is live, meaning that if you alter the stylesheet DOM after importing it, this will be reflected in the processing. It is however recommended to use stylesheet parameters instead of modifying the DOM. This is usually easier and can give better performance.

var testTransform = document.implementation.createDocument("", "test", null);
// just an example to get a transform into a script as a DOM
// XMLDocument.load is asynchronous, so all processing happens in the 
// onload handler
testTransform.addEventListener("load", onload, false);
function onload() {

importStylesheet requires one argument, a DOM Node. If that node is a document node, you can pass in a full XSL Transform or a literal result element transform, otherwise it must be an <tt>xsl:stylesheet</tt> or <tt>xsl:transform</tt> element.

Transforming the document

You can use the transformToDocument() or transformToFragment() methods to transform a document using the specified XSLT stylesheet.


transformToDocument() takes one argument, the source node to transform, and returns a new DOM Document with the results of the transformation:

var newDocument = processor.transformToDocument(domToBeTransformed);

The resultant object is an HTMLDocument if the output method of the stylesheet is <tt>html</tt>, an XMLDocument for <tt>xml</tt>, and for output method <tt>text</tt> an XMLDocument with a single root element <transformiix:result> with the text as a child.


You can also use transformToFragment() which will return a DOM DocumentFragment node. This is handy because appending a fragment to another node transparently appends all the children of that fragment, and the fragment itself is not merged. Fragments are therefore useful for moving nodes around and storing them without the overhead of a full document object.

transformToFragment takes two arguments: the source document to be transformed (as above) and the a Document object that will own the fragment (all fragments must be owned by a document).

var ownerDocument = document.implementation.createDocument("", "test", null);
var newFragment = processor.transformToFragment(domToBeTransformed, ownerDocument);

transformToFragment will only produce HTML DOM objects if the owner document is itself an HTMLDocument, or if the output method of the stylesheet is HTML. It will not produce an HTML DOM objects if only the toplevel element of the result is <html> as transformToFragment is rarely used to create this element. If you want to override this, you can set the output method normally in the standard way.

transforming HTML

Unfortunately it us currently not supported to transform HTML nodes using XSLT. Some things work if you use lower case node-names in patterns and expressions, and treat the nodes as if they are in the null namespace, however this is not very well tested so it might not work in all situations. It is also possible that this will change in a future release.

Transforming XHTML should work as expected though.

Setting parameters

You can control parameters for the stylesheet using the setParameter, getParameter, and removeParameter methods. These all take a namespace URI and a local name as the first two parameters, with setParameter taking a third - the value of the parameter to be set.


The XSLTProcessor object also implements a reset() method, which can be used to remove all stylesheets and parameters then put the processor back into its initial state. This method is implemented in Mozilla 1.3 and later.


Original Document Information

  • Author(s): Mike Hearn
  • Last Updated Date: December 21, 2005
  • Copyright Information: Copyright (C) Mike Hearn


此页面的贡献者: teoli, ziyunfei, Freeopen
最后编辑者: teoli,