Linking Using the DOM
You can apply an XSLT style sheet to an XML document or fragment by calling the transformNode
method or the transformNodeToObject
method on the source document or fragment. You can invoke these methods from a script or a COM-enabled programming language, such as C++. The style sheet and the source document or fragment are represented by instances of XML DOM. This means that you need to create two DOM instances, using the appropriate ProgID ("Msxml2.DOMDocument.5.0" for MSXML 5.0).Then, load the source document and the style sheet into the respective DOM instances before calling either of the transformation methods. The following Jscript code snippet illustrates this point:
var xmldoc = new ActiveXObject("Msxml2.DOMDocument.5.0"); var xsldoc = new ActiveXObject("Msxml2.DOMDocument.5.0"); xmldoc.load("sample.xml"); if (xmlDoc.parseError.errorCode <> 0) { var myErr = xmlDoc.parseError; alert("You have error " + myErr.reason); } else { xsldoc.load("sample.xsl"); output = xmldoc.transformNode(xsldoc); }
Alternatively, you can use:
var outdoc = new ActiveXObject("Msxml2.DomDocument.5.0"); xmldoc.transformNodeToObject(xsldoc, outdoc);
The output from the two methods differs as follows:
- transformNode
- The output of this method is a string whose value is a textual representation of the result tree. Typically, this value is HTML code for display in a Web page. Or, you can persist the result tree by saving the string into a file.
- transformNodeToObject
- This method places the result of the transformation into another object, which can then be manipulated further (as with a second transformation).
When using either method, the source object does not have to be a complete Document
object. I It can be only a node of such an object. If it is only a node, the XSLT processor treats that node, and all of its descendants, as if they were a complete document. Likewise, the XSLT object can be a complete, stand-alone XSLT file, or a node representing an embedded style sheet within an XSLT file.
See Also
transformNode Method | transformNodeToObject Method | Using XSLT with the DOM or SAX | Linking to an XSLT File | Linking Using a Processing Instruction