0
votes

What I want to do is to serialize a DOM to XML. So I create a new document

var doc = document.implementation.createDocument ('http://AOR-AppML.org', 'Application', null);

and I add nodes, attributes etc. This is working fine.

The problem is that I have different behaviours with XMLSerializer in Google Chrome and Mozilla Firefox.

Chrome console output:

<Application xmlns="http://AOR-AppML.org" name="SoRiN"><ObjectType name="ObjectTypeName"/><Enumeration name="EnumerationName"/></Application>

Firefox console output (notice the xmlns=""):

<Application xmlns="http://AOR-AppML.org" name="SoRiN"><ObjectType xmlns="" name="ObjectTypeName"/><Enumeration xmlns="" name="EnumerationName"/></Application>

I don't want to generate that empty namespace. I've read this namespaces indicates that the corresponding elements have no default namespace (http://www.w3.org/TR/xml-names/#defaulting), but actually I want them to be in the same namespace as Application.

Is there any way to prevent the namespace generation in Firefox?

P.S. - yes, I've followed the advice from this post -> How to prevent the namespace generation?

UPDATE

Here is a fiddle to play with.

1
How do you create the elements you append to the new document? According to the specifications, the answer given in the question you cited is wrong, since the method document.createElement does set the namespace URI to the empty one (w3.org/TR/DOM-Level-3-Core/core.html#ID-2141741547).Carlo Cannas
I create the elements just as suggested in the answer of that question... what other options do I have?Sorin Adrian Carbunaru
I have a added a link to a fiddle to play with.Sorin Adrian Carbunaru

1 Answers

3
votes

You have to use the method createElementNS, instead of createElement, since the latter creates an element with empty namespace URI.

Chrome serializes incorrectly the document (if you parse the string you would get a different document, with namespace URIs wrong), Firefox does the job right. Actually a bug was filed and marked as solved, but the problem seems to be still there.

So, simply replace doc.createElement(yourElementName) with doc.createElementNS('http://AOR-AppML.org', yourElementName).