How to remove XML namespaces using Javascript?

I find that for my purposes, XML namespaces just cause a lot of headaches and are completely unnecessary. (For example, how they complicate xpath.)

Is there an easy way to completely remove namespaces from an XML document?

(There is a related question, but it is about removing namespace prefixes in tags, not namespace declarations from the document root: " An easy way to drop XML namespaces using javascript ").

Edit: Examples and in more detail below:

XML:

<?xml version="1.0" ?>
<main xmlns="example.com">
  <primary>
    <enabled>true</enabled>
  </primary>
  <secondary>
    <enabled>false</enabled>
  </secondary>
</main>

JavaScript:

function useHttpResponse()
{
    if (http.readyState == 4)
    {
        if(http.status == 200)
        {
            var xml = http.responseXML;
            var evalue = getXMLValueByPath('/main/secondary/enabled', xml);
            alert(evalue);
        }
    }
}

function getXMLValueByPath(nodepath, xml)
{
    var result = xml.evaluate(nodepath, xml, null, XPathResult.STRING_TYPE, null).stringValue;
    return result;
}

XML , , . , . , .

JavaScript - ajax. xmlns="example.com" main, . - , undefined.

2:

, , XML (, ). XML , , , . , , : " XML Javascript?" , 1) 2) node, xpath.

+5
2

, :

var xml = http.responseXML.replace(/<([a-zA-Z0-9 ]+)(?:xml)ns=\".*\"(.*)>/g, "<$1$2>");
+5

xmlns javascript XML

xmlns=\"(.*?)\"

NB:

var str = `<?xml version="1.0" ?>
<main xmlns="example.com">
  <primary>
    <enabled>true</enabled>
  </primary>
  <secondary>
    <enabled>false</enabled>
  </secondary>
</main>`;

str = str.replace(/xmlns=\"(.*?)\"/g, '');

console.log(str)
Hide result
+1

All Articles