Thanks, it works. But now I have another problem. Some third-party library generates XML elements, which are added to my XML document. Each element contains _explicit_ namespace declarations. When document is serialized it looks like I posted earlier (see below). The problem is that even if I add namespace declaration to the top-level element, resulting document looks like this in serialized form:

<ns:root xmlns:ns="urn:ns1" xmlns:ns2="urn:ns2">
    <ns2:child xmlns:ns2="urn:ns2"/>
    <ns2:child xmlns:ns2="urn:ns2"/>
</ns:root>

Is there any way to automatically force redunant namespace declarations to be removed from the child elements when they are serialized?

Stanimir Stamenkov wrote:
/Igor Lobanov/:

<ns1:root xmlns:ns1="urn:ns1">
  <ns2:child xmlns:ns2="urn:ns2"/>
  <ns2:child xmlns:ns2="urn:ns2"/>
</ns1:root>

I guess the above result is because of specific normalization / optimization algorithm - the serialization would need to look ahead which might not be always possible to produce the form you want. I don't know if the namespace normalization algorithm [1] for the DOM Level 3 Document.normalizeDocument() is any different, too. You would notice descendant elements of <ns2:child> which are in the same namespace won't get 'xmlns:ns2' attribute.

You could always add the namespace declaration attribute yourself:

    root.setAttributeNS("http://www.w3.org/2000/xmlns/";,
                        "xmlns:ns2", "urn:ns2");

[1] http://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#normalizeDocumentAlgo


--
Igor Lobanov
Internal Development Engineer
SWsoft, Inc.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to