This is my sample XML
<?xml version="1.0"?>
<DOCUMENT>
<MY_DATA_SET xmlns="">
<MY_DATA>
<MY_DATA>
</MY_DATA_SET>
<DOCUMENT>
I want to remove programmatically this part xmlns=""
output should be
<?xml version="1.0"?>
<DOCUMENT>
<MY_DATA_SET>
<MY_DATA>
<MY_DATA>
</MY_DATA_SET>
<DOCUMENT>
- let theDocument = getXmlNode(XML,'//DOCUMENT');
(getXMLNode is a function it can give docuemnt node element)
- let testNode = create('<MY_DATA><MY_DATA></MY_DATA></MY_DATA>');
(create the XML from string)
- let firstChild = theDocument._domNode._firstChild;
(getting the first child from the document)
theDocument.node.insertBefore(testNode.node._documentElement, firstChild);
output is :
<?xml version="1.0"?>
<DOCUMENT>
<MY_DATA_SET xmlns="">
<MY_DATA>
<MY_DATA>
</MY_DATA_SET>
<DOCUMENT>
I don want to see this part : xmlns=""
if i try to remove :testNode.node._documentElement.removeAttribute("xmlns");
it didn’t work
2
Answers
If you have well-formed XML like
then, to get rid of the superfluous empty XML namespace declaration, it suffices to run the XML through an XSLT identity transformation, can be done with XSLT 3.0 and the XSLT code
and SaxonJS
You can sort of "fake" the removal by creting a new namespace-free document and inserting into it the child data.
Note that both your sample and expected xml are not well formed and the code below fixes that: