Creating an XML DOM element while maintaining case sensitivity

I am trying to create the following nodetree element:

<v:custProps> <v:cp v:nameU="Cost"> </v:custProps> 

from:

 newCustprop = document.createElement("v:custProps"); newcp = document.createElement("v:cp"); newcp.setAttribute("v:nameU", "Cost"); newCustprop.appendChild(newcp); 

However, document.createElement("v:custProps") generates <v:custprops> unlike <v:custprops> . Is there any way to avoid this parsing?


Change 1:

I am currently reading this this article on case sensitivity nodename. This is slightly irrelevant to my problem, although due to the fact that my code is not debugged with <![CDATA]]> , I would prefer not to use .innerHTML .

+5
source share
2 answers

You need to use createElementNS() / setAttributeNS() and provide a namespace, not just an alias / prefix. The example uses urn:v as a namespace.

 var xmlns_v = "urn:v"; var newCustprop = document.createElementNS(xmlns_v, "v:custProps"); var newcp = document.createElementNS(xmlns_v, "v:cp"); newcp.setAttributeNS(xmlns_v, "v:nameU", "Cost"); newCustprop.appendChild(newcp); var xml = (new XMLSerializer).serializeToString(newCustprop); 

XML:

 <v:custProps xmlns:v="urn:v"><v:cp v:nameU="Cost"/></v:custProps> 
+3
source

Using document.createElement for qualified names is not recommended. See if document.createElementNS can serve you better.

+2
source

Source: https://habr.com/ru/post/1207070/


All Articles