在Javascript中创建带前缀的XML节点是否可行?

时间:2015-06-25 13:59:25

标签: javascript xml xslt

我试图创建一个带有名称空间或前缀的xml文件。

<bpmndi:BPMNDiagram id="BPMNDiagram_1"> <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1"> <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1"> <dc:Bounds x="173" y="102" width="36" height="36" /> </bpmndi:BPMNShape> <bpmndi:BPMNShape id="Task_1_di" bpmnElement="Task_1"> <dc:Bounds x="437" y="107" width="100" height="80" /> </bpmndi:BPMNShape> <bpmndi:BPMNEdge id="SequenceFlow_1_di" bpmnElement="SequenceFlow_1"> <di:waypoint xsi:type="dc:Point" x="209" y="120" /> <di:waypoint xsi:type="dc:Point" x="323" y="120" /> <di:waypoint xsi:type="dc:Point" x="323" y="147" /> <di:waypoint xsi:type="dc:Point" x="437" y="147" /> <bpmndi:BPMNLabel> <dc:Bounds x="278" y="123.5" width="90" height="20" /> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram>

我尝试使用document.createElement(&#34; bpmn&#34;);但是我无法设置前缀。

谢谢!

3 个答案:

答案 0 :(得分:0)

您可以使用:

  

nodeObject.prefix =前缀

将返回:

  

nodeObject.prefix

答案 1 :(得分:0)

document.createElementNS,请参阅http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-DocCrElNS,您可以使用var el = document.createElementNS('http://your-namespace-uri-here', 'prefix:localnamehere')。应该在DOM Level 2或3实现(如Mozilla或Opera或Chrome)或新版本的IE for XML DOM文档中工作。

var ns1 = 'http://example.com/ns1';
var ns2 = 'http://example.org/ns2';

var doc = document.implementation.createDocument(ns1, 'pf1:root', null);

var el1 = doc.createElementNS(ns1, 'pf1:foo');
el1.setAttribute('id', 'e1');
doc.documentElement.appendChild(el1);

var el2 = doc.createElementNS(ns2, 'pf2:bar');
el1.appendChild(el2);

var pre = document.createElement('pre');
pre.textContent = new XMLSerializer().serializeToString(doc);

document.body.appendChild(pre);

在旧版本的IE中,XML DOM仅受MSXML和ActiveX支持,您需要使用createNode方法,请参阅https://msdn.microsoft.com/en-us/library/ms757901%28v=vs.85%29.aspx

答案 2 :(得分:0)

我认为我走在正确的轨道上......这是我的代码。它与createElementNS。

感谢马丁:)。

var container = document.createElement("root");
var bpmn = document.createElementNS('http://your-namespace-uri-here','bpmn:definitions');
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xsi","http://ww w.w3.org/2001/XMLSchema-instance");
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:bpmn","http://www.omg.org/spec/BPMN/20100524/MODEL");
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:bpmndi","http://www.omg.org/spec/BPMN/20100524/DI");
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:di","http://www.omg.org/spec/DD/20100524/DI"); 
    bpmn.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:dc","http://www.omg.org/spec/DD/20100524/DC");

var process = document.createElementNS("task",'bpmn:process');
process.id="Process_1";
process.setAttribute("isExecutable","false");

var sequenceFlow = document.createTextNode("sequenceFlow_1");

var start = document.createElementNS("task",'bpmn:startEvent');
start.id="StartEvent_1";
var outgoing = document.createElementNS("task",'bpmn:outgoing');
outgoing.appendChild(sequenceFlow)  
start.appendChild(outgoing);

var sequenceFlow1 = document.createTextNode("sequenceFlow_1");
var incoming = document.createElementNS("incoming","bpmn:incoming");
incoming.appendChild(sequenceFlow1)

var task = document.createElementNS("task","bpmn:task");
task.id = "Task_1";
task.setAttribute("name","Titulo112");

task.appendChild(incoming);
process.appendChild(start);
process.appendChild(task);
bpmn.appendChild(process);

container.appendChild(bpmn);

console.debug(容器);