skip to Main Content

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


  1. If you have well-formed XML like

    <DOCUMENT>
     <MY_DATA_SET xmlns="">
      <MY_DATA>
      </MY_DATA>
     </MY_DATA_SET>
    </DOCUMENT>
    

    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

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="#all">
    
      <xsl:mode on-no-match="shallow-copy"/>
      
    </xsl:stylesheet>
    

    and SaxonJS

    const SaxonJS = require("saxon-js");
    
    const xml = `<DOCUMENT>
     <MY_DATA_SET xmlns="">
      <MY_DATA>
      </MY_DATA>
     </MY_DATA_SET>
    </DOCUMENT>`;
    
    const xslt = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="#all">
    
      <xsl:mode on-no-match="shallow-copy"/>
      
    </xsl:stylesheet>`;
    
    const serializedResult = SaxonJS.XPath.evaluate(`transform(map{ 'source-node' : parse-xml($xml), 'stylesheet-text' : $xslt, 'delivery-format' : 'serialized' })?output`, null, { params : { xml : xml, xslt : xslt } });
    
    console.log(serializedResult);
    
    Login or Signup to reply.
  2. 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:

    source = `<?xml version="1.0"?> 
    <DOCUMENT>
     <MY_DATA_SET xmlns="">
      <MY_DATA/>
      <MY_DATA/>
     </MY_DATA_SET>
    </DOCUMENT>
    `
    newdoc = `<?xml version="1.0"?> 
    <DOCUMENT>
     <MY_DATA_SET> 
     </MY_DATA_SET>
    </DOCUMENT>
    `
    domdocOld = new DOMParser().parseFromString(source, "text/xml")
    domdocnew = new DOMParser().parseFromString(newdoc, "text/xml")
    
    dest = domdocnew.evaluate('//MY_DATA_SET', domdocnew, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    
    newdat = `'<MY_DATA/><MY_DATA/>
        `
    dest.snapshotItem(0).insertAdjacentHTML('beforeend', newdat);
    
    console.log(new window.XMLSerializer().serializeToString(domdocnew));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search