This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The transformToFragment() method of the XSLTProcessor interface transforms a provided Node source to a DocumentFragment using the XSLT stylesheet associated with the XSLTProcessor.
transformToFragment(source, document)
sourceThe Node source to apply the XSLT stylesheet to.
documentThe Document the document fragment will be associated with. (Any document fragment is associated with a document it can be added to).
This example demonstrates how to use transformToFragment() to transform XML data into HTML, which can then be directly inserted into the DOM as a document fragment.
<div id="result"></div>
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="html"/>
<xsl:template match="/">
<ul>
<xsl:for-each select="books/book">
<li>
<strong><xsl:value-of select="title"/></strong>
by <em><xsl:value-of select="author"/></em>
</li>
</xsl:for-each>
</ul>
</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 document fragment
const resultFragment = xsltProcessor.transformToFragment(xmlDoc, document);
// Insert the result into the page
document.getElementById("result").appendChild(resultFragment);
| Specification |
|---|
| DOM> # dom-xsltprocessor-transformtofragment> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
transformToFragment |
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/transformToFragment