Last Comment Bug 318086 - No XML Declaration when serializing
: No XML Declaration when serializing
Status: NEW
Product: Core
Classification: Components
Component: Serializers (show other bugs)
: Trunk
: All All
-- normal with 3 votes (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
: Andrew Overholt [:overholt]
Depends on:
  Show dependency treegraph
Reported: 2005-11-29 01:53 PST by Bruno Vernay
Modified: 2011-10-13 12:04 PDT (History)
6 users (show)
jst: blocking1.9-
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Description User image Bruno Vernay 2005-11-29 01:53:08 PST
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8b5) Gecko/20051103 Flock/0.4 Firefox/1.0+
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8b5) Gecko/20051103 Flock/0.4 Firefox/1.0+

When creating an XML document with Javascript, there is only a dirty hack to add an XML Declaration to it.

Reproducible: Always

    var doc = document.implementation.createDocument("", "", null);
    var rootElem = doc.createElement("toto");
    rootElem.setAttribute("version", "12334");
    var serializer = new XMLSerializer();
    serializer.serializeToStream(doc, foStream, "UTF-8");
Result in no XML Declarations (there should be one.)

Dirty hack :
   var piElem = doc.createProcessingInstruction("xml", "version='1.0'
    doc.insertBefore(piElem, rootElem);

Note :
 - you can claim UTF-8 and encode in ISO-8859-1
 - a XML Declaration is not a Processing Instruction

See :
Comment 1 User image Boris Zbarsky [:bz] (still a bit busy) 2007-02-05 10:06:03 PST
So yeah.  The DOM doesn't allow specifying the info in the XML declaration at document creation time, sadly.

But really, the main issue here is the encoding problem.  The current setup in nsXMLContentSerializer::AppendDocumentStart overloads the version to see whether there was a declaration or not.

I think we should do the following:

1)  Change nsIDocument::GetXMLDeclaration to always output the version, etc and
    return a boolean indicating whether there was an actual declaration.
2)  If serializing to UTF8 and there was no declaration, we should continue not
    producing a declaration, imo.
3)  If serializing to some other charset we MUST produce a declaration to create
    valid XML.
4)  Documents created via createDocument() should have

Comment 2 User image Johnny Stenback (:jst, 2007-02-08 17:04:29 PST
Not a blocker.

Note You need to log in before you can comment on or make changes to this bug.