سؤال

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.

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top