Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML Declaration

5 views
Skip to first unread message

bruno...@gmail.com

unread,
Nov 25, 2005, 7:10:17 PM11/25/05
to
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

Martin Honnen

unread,
Nov 27, 2005, 9:11:08 PM11/27/05
to

bruno...@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?

--

Martin Honnen
http://JavaScript.FAQTs.com/

bruno vernay

unread,
Nov 28, 2005, 2:06:53 PM11/28/05
to
No, using serializer.serializeToStream(doc, foStream, "ISO-8859-1");
change the encoding, but doesn't put an XML Declaration !

Maybe I should post to netscape.public.mozilla.dom too ?

or fill a bug or a feature request ?

Thanks
Bruno

bruno vernay

unread,
Nov 28, 2005, 2:16:06 PM11/28/05
to
Dirty hack :
var piElem = doc.createProcessingInstruction("xml", "version='1.0'
encoding='UTF8'");
...
doc.appendChild(rootElem);
doc.insertBefore(piElem, rootElem);

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

Jonas Sicking

unread,
Nov 29, 2005, 3:33:59 AM11/29/05
to
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.

/ Jonas

bruno vernay

unread,
Nov 29, 2005, 3:25:19 PM11/29/05
to
Done : https://bugzilla.mozilla.org/show_bug.cgi?id=318086
In fact it is working.
Fell free to vote for this bug (feature request.)
Thanks

Martin Honnen

unread,
Nov 29, 2005, 9:39:50 PM11/29/05
to

Jonas Sicking wrote:


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."

But even with that if we look at <http://www.w3.org/TR/REC-xml/#NT-Name>
there are no restrictions for XML names itself to be e.g. "xml".

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 have made a test case here:
<http://home.arcor.de/martin.honnen/mozillaBugs/domLevel2/CreateXMLDeclarationAsProcessingInstruction1.html>
It is easy filing a bug with that but I am not sure about the component,
i.e. DOM with createProcessingInstruction or XML with XMLSerializer.

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".

Martin Honnen

unread,
Nov 29, 2005, 10:26:46 PM11/29/05
to

Martin Honnen wrote:


> I have made a test case here:
> <http://home.arcor.de/martin.honnen/mozillaBugs/domLevel2/CreateXMLDeclarationAsProcessingInstruction1.html>
>
> It is easy filing a bug with that but I am not sure about the component,
> i.e. DOM with createProcessingInstruction or XML with XMLSerializer.

Further test case here:
<http://home.arcor.de/martin.honnen/mozillaBugs/domLevel2/CreateXMLDeclarationAsProcessingInstruction2.html>
which creates two processing instructions looking like XML declarations
so that the serialized markup is not well-formed then.

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.

bruno vernay

unread,
Nov 29, 2005, 11:03:26 PM11/29/05
to
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.

Jonas Sicking

unread,
Nov 30, 2005, 1:11:35 AM11/30/05
to
>>> 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.

/ Sicking

Heikki Toivonen

unread,
Nov 30, 2005, 12:25:26 PM11/30/05
to
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

bruno vernay

unread,
Nov 30, 2005, 3:57:34 PM11/30/05
to
Do you mean XML Declaration ?? It is in DOM 3 (I think)

Looks like it should be possible to set xmlVersion :
http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-version

It is said that the serializer can write (or not write the XML
Declaration) :
http://www.w3.org/TR/DOM-Level-3-LS/load-save.html#parameter-xml-declaration

Anyway I would like some Javascript methods like
http://www.cafeconleche.org/books/xmljava/chapters/ch13s03.html

Martin Honnen

unread,
Nov 30, 2005, 7:43:42 PM11/30/05
to

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.

That sounds like some misunderstanding. In terms of the XML
specification and it terms of the W3C DOM the XML declaration is not a
processing instruction even if the markup delimiters are the same.
If you load XML from source and the W3C DOM is built then the XML
declaration does not show up in the DOM as a child node at all,
certainly not as a processing instruction node.
In DOM Level 3 core some info from the XML declaration should be exposed
as properties of the document object, namely
xmlVersion
<http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-version>
xmlEncoding
<http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-encoding>
xmlStandalone
<http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-standalone>

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/LSSerializerXMLDeclaration1.html>

Heikki Toivonen

unread,
Dec 17, 2005, 7:18:27 AM12/17/05
to
Martin Honnen wrote:
> 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.
>
> 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.

--
Heikki Toivonen

0 new messages