Bug 10419 - XSLTProcessor transformToFragment fails because of an XML declaration
Summary: XSLTProcessor transformToFragment fails because of an XML declaration
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: XML (show other bugs)
Version: 420+
Hardware: Mac OS X 10.4
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-08-15 09:53 PDT by j_mckitrick
Modified: 2006-08-26 11:15 PDT (History)
1 user (show)

See Also:


Attachments
test case (3.05 KB, application/zip)
2006-08-16 02:50 PDT, Alexey Proskuryakov
no flags Details
inhibit XML declarations (39.87 KB, patch)
2006-08-21 11:50 PDT, Alexey Proskuryakov
eric: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description j_mckitrick 2006-08-15 09:53:00 PDT
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.
Comment 1 Alexey Proskuryakov 2006-08-15 10:51:33 PDT
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.
Comment 2 j_mckitrick 2006-08-15 14:57:15 PDT
---------- 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
Comment 3 Alexey Proskuryakov 2006-08-16 02:50:22 PDT
Created attachment 10049 [details]
test case
Comment 4 Alexey Proskuryakov 2006-08-16 02:59:34 PDT
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.
Comment 5 j_mckitrick 2006-08-16 09:31:38 PDT
(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?
Comment 6 Alexey Proskuryakov 2006-08-21 11:29:05 PDT
(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).
Comment 7 Alexey Proskuryakov 2006-08-21 11:50:40 PDT
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 8 Eric Seidel (no email) 2006-08-21 16:11:16 PDT
Comment on attachment 10147 [details]
inhibit XML declarations

Looks good.  r=me.
Comment 9 Alexey Proskuryakov 2006-08-21 21:25:25 PDT
Committed revision 15960.
Comment 10 j_mckitrick 2006-08-26 06:19:00 PDT
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?
Comment 11 Alexey Proskuryakov 2006-08-26 11:15:47 PDT
See <http://www.sagehill.net/docbookxsl/OutputEncoding.html> (found with Google).