This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The transformToDocument() method of the XSLTProcessor interface transforms the provided Node source to a Document using the XSLT stylesheet associated with XSLTProcessor.
transformToDocument(source)
A Document. The actual interface depends on the output method of the stylesheet, as specified by <xsl:output> element's method.
| Output method | Result interface |
|---|---|
html | HTMLDocument |
xml | XMLDocument |
text |
XMLDocument with a single root element <transformiix:result> with the text as a child |
This example demonstrates how to use transformToDocument() to transform an XML document using XSLT, resulting in a new XML document structure.
<pre id="result"></pre>
const xmlString = `
<books>
<book>
<title>Book 1</title>
<author>Author 1</author>
</book>
<book>
<title>Book 2</title>
<author>Author 2</author>
</book>
</books>
`;
const xsltString = `
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<catalog>
<xsl:for-each select="books/book">
<item>
<name><xsl:value-of select="title"/></name>
<writer><xsl:value-of select="author"/></writer>
</item>
</xsl:for-each>
</catalog>
</xsl:template>
</xsl:stylesheet>
`;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
const xsltDoc = parser.parseFromString(xsltString, "application/xml");
const xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
// Perform the transformation, returning the result as a new XML document
const resultDoc = xsltProcessor.transformToDocument(xmlDoc);
// Serialize the result document to a string
const serializer = new XMLSerializer();
const resultString = serializer.serializeToString(resultDoc);
// Display the transformed XML in the page
document.getElementById("result").textContent = resultString;
| Specification |
|---|
| DOM> # dom-xsltprocessor-transformtodocument> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
transformToDocument |
1Chrome returnsnull if an error occurs. |
12Edge returnsnull if an error occurs. |
1Firefox throws an exception if an error occurs. |
≤12.1["Opera 12.1 and earlier throws an exception if an error occurs.", "Opera 15 and later returnsnull if an error occurs."] |
3.1Safari returnsnull if an error occurs. |
18Chrome Android returnsnull if an error occurs. |
4Firefox for Android throws an exception if an error occurs. |
≤12.1["Opera Android 12.1 and earlier throws an exception if an error occurs.", "Opera Android 14 and later returnsnull if an error occurs."] |
2Safari on iOS returnsnull if an error occurs. |
1.0Samsung Internet returnsnull if an error occurs. |
3WebView returnsnull if an error occurs. |
2WebView on iOS returnsnull if an error occurs. |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XSLTProcessor/transformToDocument