2012-02-06 69 views
0

我正在尝试将属性添加到xml节点中。我创建了以下功能XML节点:使用名称空间添加属性

function AddAttribute(xmlNode, attrname, attrvalue, path) { 
    var attr; 
    if (isIE()) 
     attr = xmlNode.ownerDocument.createNode(2, attrname, "http://mydomain/MyNameSpace"); 
    else 
     attr = xmlNode.ownerDocument.createAttributeNS("http://mydomain/MyNameSpace", attrname); 

    attr.nodeValue = attrvalue; 
    var n = xmlNode.selectSingleNode(path); 
    n.setAttributeNode(attr); 
} 

此代码在Firefox中不可用。它添加节点,但不添加名称空间。 我已经尝试在IE和Chrome中,它工作正常。

你知道我该如何添加命名空间吗? 或者您是否知道使用命名空间创建属性的其他选择?

谢谢

+0

你作为'attrname'传递了什么? – Tomalak 2012-02-06 13:04:36

+0

我通过:“co:internalcollectiontype” – SergioKastro 2012-02-06 13:06:13

+0

我找到了一个解决方案(可能不是最好的)。我不能作为答复发布,我需要等待8个小时。直到然后这里是我的意见: var n = xmlNode.selectSingleNode(path); if(cb.browser.ie)// IE n.setAttributeNode(attr);其他 n.setAttributeNodeNS(attr); – SergioKastro 2012-02-06 13:31:45

回答

1

我找到了一个可能的解决方案。至少现在适用于三种浏览器:IE,Firefox和Chrome。

function AddAttribute(xmlNode, attrname, attrvalue, path) { 
    var attr; 
    if (xmlNode.ownerDocument.createAttributeNS) 
     attr = xmlNode.ownerDocument.createAttributeNS("http://www.firmglobal.com/MyNameSpace", attrname); 
    else 
     attr = xmlNode.ownerDocument.createNode(2, attrname, "http://www.firmglobal.com/MyNameSpace"); 

    attr.nodeValue = attrvalue; 
    var n = xmlNode.selectSingleNode(path); 

    //Set the new attribute into the xmlNode 
    if (n.setAttributeNodeNS) 
     n.setAttributeNodeNS(attr); 
    else 
     n.setAttributeNode(attr); 
} 

感谢“Tomalak”的帮助。

相关问题