Here is an example of applying an XSLT stylesheet 'x' to source xml node 'x': var t = new XSLTProcessor(); t.importStylesheet(x); var f = t.transformToFragment(s, document); d.innerHTML = ""; d.appendChild(f); In every case I have tried, the variable 'f' is NULL. A fragment is never returned.
We've got a bunch of working examples in layout tests (e.g. fast/xsl/xslt-processer.html). Please provide a test case that illustrates the problem you are seeing.
---------- file: xslt-frag.xml ---------- <?xml version="1.0" encoding="UTF-8"?> <report-types> <report-type>Report 1</report-type> <report-type>Report 2</report-type> <report-type>Report 2</report-type> </report-types> ---------- file: xslt-frag.xsl ---------- <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/report-types"> <table> <tr> <th>Options</th> </tr> <tr> <xsl:for-each select="report-type"> <td><xsl:value-of select="."/></td> </xsl:for-each> </tr> </table> </xsl:template> </xsl:stylesheet> ---------- test case ---------- // JCM var xml3 = getXMLDocument("xslt-frag.xml"); addXMLResult("xml source node", xml3); var xsl3 = getXMLDocument("xslt-frag.xsl"); addXMLResult("xsl transform node", xsl3); var p = new XSLTProcessor; p.importStylesheet(xsl3); var ownerDocument = document.implementation.createDocument("", "test", null); var f = p.transformToFragment(xml3, ownerDocument); alert(serializer.serializeToString(f)); //addResultExpectValueWhenSerialized("transformed fragment is table", f, serializer.serializeToString); // JCM
Created attachment 10049 [details] test case
Apparently, createFragmentFromSource() chokes on the XML declaration in a string produced by transformToString(), and rightfully so. To work around this problem while we are fixing it, you can inhibit XML declaration printout: <xsl:output method="xml" omit-xml-declaration="yes"/> Of course, this is not an issue with HTML fragments.
(In reply to comment #4) > Apparently, createFragmentFromSource() chokes on the XML declaration in a > string produced by transformToString(), and rightfully so. > > To work around this problem while we are fixing it, you can inhibit XML > declaration printout: > > <xsl:output method="xml" omit-xml-declaration="yes"/> > > Of course, this is not an issue with HTML fragments. > Excellent! It works just fine for now. Even though you will fix the bug, perhaps the output declaration would be a good idea anyway for documentation purposes. I also changed the method to html and found that fixed a couple of other issues. So maybe that's the best way to deal with it?
(In reply to comment #5) > I also changed the method to html and found that fixed a couple of other > issues. So maybe that's the best way to deal with it? I think this depends on the kind of your target document (whether it's HTML or XHTML).
Created attachment 10147 [details] inhibit XML declarations Inhibit XML declaration printout in XSLTProcessor::transformToString(). Unless I'm missing something, this shouldn't affect anything but line numbers in error messages, which would decrease by one.
Comment on attachment 10147 [details] inhibit XML declarations Looks good. r=me.
Committed revision 15960.
I'm having one last issue: If I want xml:output to be HTML, how do I include an encoding? I have no problem outputting XML as UTF-8, but how do I do the same with HTML?
See <http://www.sagehill.net/docbookxsl/OutputEncoding.html> (found with Google).