简体   繁体   English

使用JavaScript或jQuery创建区分大小写或'camelCase'的XML节点

[英]Creating case-sensitive or 'camelCase' XML nodes using JavaScript or jQuery

I need to create some case-sensitive XML nodes in JavaScript which have attribute names with a colon in between them. 我需要在JavaScript中创建一些区分大小写的XML节点,这些节点的属性名称之间带有冒号。

Example: <Data ss:Type="String">Contact Name</Data> 示例: <Data ss:Type="String">Contact Name</Data>

When I try to create an element via JavaScript with the createElement() function it creates it in lowercase. 当我尝试使用createElement()函数通过JavaScript创建元素时,它会以小写形式创建它。 Also, there are a lot of problems adding the attributes with a colon in them. 此外,添加带冒号的属性存在很多问题。 For example: ss:Type="String" 例如: ss:Type="String"

This is Excel data, and I am saving the the source .xml file back to an .xls file. 这是Excel数据,我将源.xml文件保存回.xls文件。 The case of the XML elements and attributes is very important for Excel to be able to read the file. XML元素和属性的大小写对于Excel能够读取文件非常重要。

Any examples or pointers will be great help. 任何示例或指针都会有很大帮助。

The JavaScript methods you are looking for are document.createElementNS and elm.setAttributeNS 您正在寻找的JavaScript方法是document.createElementNSelm.setAttributeNS

var e = document.createElementNS('http://www.microsoft.com/office/excel/', 'Data');
e.setAttributeNS('http://www.microsoft.com/office/excel/', 'ss:Type', 'String');
e.textContent = 'Contact Name';

Then if you want to get strings back, .innerHTML won't work anymore so you have to use an XMLSerializer 然后,如果您想找回字符串, .innerHTML将不再起作用,因此您必须使用XMLSerializer

var s = new XMLSerializer();
s.serializeToString(e);

The answer by Paul S. lead me to a solution. Paul S.的回答使我找到了解决方案。

I created the required XML DOM elements with createElement() and added the required attributes with setAttribute() . 我使用createElement()创建了所需的XML DOM元素,并使用setAttribute()添加了所需的属性。 This resulted in: 这导致:

<data ss:type="String">Contact Name</data>

Note that the element and attribute name in this example are both lower-case . 请注意,此示例中的元素和属性名称均为小写 However, what I really wanted was Pascal case : 但是,我真正想要的是Pascal案例

<Data ss:Type="String">Contact Name</Data>

After first converting the XML DOM to a string via XMLSerializer() as suggested by Paul S., I then simply used replace() to convert all occurrences of <data to <Data and ss:type to ss:Type . XMLSerializer() Paul S.的建议首先通过XMLSerializer()将XML DOM转换为字符串之后,我只需使用replace()将所有出现的<data转换为<Datass:typess:Type

The solution to my question are XMLSerializer() and String replace() function 我的问题的解决方案是XMLSerializer()和String replace()函数

It's a dated question but i'll give my answer: 这是一个过时的问题,但我会给出答案:
When using window.document to create elements you are using an HTMLDocument (with ns="http://www.w3.org/1999/xhtml") and nodes created with it responds to html standards with the limitations you experienced. 当使用window.document创建元素时,您正在使用HTMLDocument (使用ns =“http://www.w3.org/1999/xhtml”),并且使用它创建的节点会响应具有您遇到的限制的html标准。
You should use a Document instead to handle generic XML. 您应该改用Document来处理通用XML。

// instantiate a new Document  
var xml_doc = document.implementation.createDocument('optional_namespace','rootElement');  

//  then your code to create element  
var e = xml_doc.createElementNS('http://www.microsoft.com/office/excel/', 'Data');  
e.setAttributeNS('http://www.microsoft.com/office/excel/', 'ss:Type', 'String');  
e.textContent = 'Contact Name';  

// probably append it  
xml_doc.documentElement.appendChild(e);  

// serialized xml_doc:  
var s = new XMLSerializer();  
s.serializeToString(xml_doc);  

/*
<rootElement xmlns="optional_namespace">
  <Data xmlns="http://www.microsoft.com/office/excel/" xmlns:ss="http://www.microsoft.com/office/excel/" ss:Type="String">Contact Name</Data>
</rootElement>"
*/

EDIT : tested in Chrome and FF 编辑 :在Chrome和FF中测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM