I would like to add an XML declaration to a Javascript generated Document :
var doc = document.implementation.createDocument("", "", null); var rootElem = doc.createElement("toto"); rootElem.setAttribute("version", "12334"); doc.appendChild(rootElem); var serializer = new XMLSerializer(); serializer.serializeToStream(doc, foStream, "UTF-8");
I get no <?xml ..?> at the beginning of the file ! I tried to appenChild a createProcessingInstruction("xml", "version='1.0' encoding='UTF8'"); But with no success. Thanks Bruno
brunover...@gmail.com wrote: > I would like to add an XML declaration to a Javascript generated > Document :
> var doc = document.implementation.createDocument("", "", null); > var rootElem = doc.createElement("toto"); > rootElem.setAttribute("version", "12334"); > doc.appendChild(rootElem);
Note that you can shorten that to var doc = document.implementation.createDocument("", "toto", null); doc.documentElement.setAttribute("version", "12334");
> var serializer = new XMLSerializer(); > serializer.serializeToStream(doc, foStream, "UTF-8");
> I get no <?xml ..?> at the beginning of the file !
Do you get one if you serialize to an encoding like ISO-8859-1 where the XML declaration is mandatory then?
bruno vernay wrote: > Dirty hack : > var piElem = doc.createProcessingInstruction("xml", "version='1.0' > encoding='UTF8'"); > - a XML Declaration is not a Processing Instruction > Real dirty ...
Yeah, it is in fact so dirty that it should not work. Could you file a bug in bugzilla about that and post the bug number here.
>> - a XML Declaration is not a Processing Instruction >> Real dirty ...
> Yeah, it is in fact so dirty that it should not work. Could you file a > bug in bugzilla about that and post the bug number here.
Where is the bug in your view, should the DOM not allow creating such a processing instruction or should the serializer bark at that processing instruction when serializing the document?
Unfortunately the DOM in W3C DOM Level 2 for createProcessingInstruction is not too specific on what kind of target arguments should throw errors. <http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document> says: "Exceptions DOMException INVALID_CHARACTER_ERR: Raised if the specified target contains an illegal character."
DOM Level 3 (although Mozilla does not support much of that so far) is a bit more specific. <http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-135944439> says: "Exceptions DOMException INVALID_CHARACTER_ERR: Raised if the specified target is not an XML name according to the XML version in use specified in the Document.xmlVersion attribute."
Only <http://www.w3.org/TR/REC-xml/#sec-pi> clearly excludes e.g. "xml" or "XML" as target names for processing instructions but there is nothing in the DOM spec saying that createProcessingInstruction should implement that rule.
None of the other DOM implementations I have tested (the above test case with IE 6 and MSXML, with Opera 8.5 and Opera 9 Preview; a short Java test program using the built-in DOM in Java 1.4 and 1.5) throw an error on createProcessingInstruction with target "xml".
Neither Mozilla (tested now with Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20051129 Firefox/1.6a1) nor other browsers give any errors when creating the DOM. Also no error when serializing.
In fact it is a chance that Firefox doesn't throw an exception when creating a "wrong" Processing Instruction. Because it allow us to overcome the real problem which is Serializing doesn't write an XML Declaration. I hesitate to name it an enhancement, but I saw other features (XML Load & save for example) marked as bug, so I marked it as bug too, but it can be seen as a feature request.
>>> Dirty hack : >>> var piElem = doc.createProcessingInstruction("xml", "version='1.0' >>> encoding='UTF8'"); >>> - a XML Declaration is not a Processing Instruction >>> Real dirty ...
>> Yeah, it is in fact so dirty that it should not work. Could you file a >> bug in bugzilla about that and post the bug number here.
> Where is the bug in your view, should the DOM not allow creating such a > processing instruction or should the serializer bark at that processing > instruction when serializing the document?
IMHO the bug is in that we allow such a PI to be created.
> Only <http://www.w3.org/TR/REC-xml/#sec-pi> clearly excludes e.g. "xml" > or "XML" as target names for processing instructions but there is > nothing in the DOM spec saying that createProcessingInstruction should > implement that rule.
I believe the intention of the DOM WG was to disallow illegal PI-names, both in DOM L2 and DOM L3. Even though I agree they are very unclear on that.
Martin Honnen wrote: > Neither Mozilla (tested now with Mozilla/5.0 (Windows; U; Windows NT > 5.1; en-US; rv:1.9a1) Gecko/20051129 Firefox/1.6a1) nor other browsers > give any errors when creating the DOM. Also no error when serializing.
Given that there is no other way to create the processing instruction (right?), I would hate to "fix" this by making it impossible to create the processing instruction.
Anybody know what you are supposed to do in DOM to create a processing instruction? What will DOM 4 say?
Heikki Toivonen wrote: > Given that there is no other way to create the processing instruction > (right?), I would hate to "fix" this by making it impossible to create > the processing instruction.
The current problem in this thread is originally that XMLSerializer and its serialization methods do not seem to create an XML declaration when serializing a DOM document and that a "hack to fix that" by creating and inserting a processing instruction node in the DOM looking like an XML declaration when being serialized is possible. But Jonas thinks that hack should not work and be fixed. If Bruno is right that serializer.serializeToStream(doc, foStream, "ISO-8859-1") does not create an XML declaration then I think the method should be fixed first to ensure an XML declaration properly declares the encoding the serialized markup has.
> Anybody know what you are supposed to do in DOM to create a processing > instruction?
Of course you can create a processing instruction with the method createProcessingInstruction. The question however is how you can make sure serialization of a DOM tree has an XML declaration without needing the above "hack" of putting in a processing instruction in the DOM looking like the XML declaration when being serialized. DOM Level 3 Load and Save for LSSerializer allows to set a property named xml-declaration to have one. Only Opera (8.xy release versions and the version 9 preview) implement DOM Level 3 Load and Save but setting that property does not work so far <http://home.arcor.de/martin.honnen/operaBugs/op9/DOMLevel3/LSSerializ...>
>> Given that there is no other way to create the processing instruction >> (right?), I would hate to "fix" this by making it impossible to create >> the processing instruction.
> That sounds like some misunderstanding. In terms of the XML
Argh, I mixed processing instruction and XML declaration. I meant to talk about XML declaration. As far as I was aware it was not possible to create XML declarations with DOM except by processing instruction misuse. But I don't know DOM 3, and based on the links you provided it does seem like DOM 3 will provide at least some help here. Sorry for the confusion.