Javascript XML节点:添加具有命名空间的属性

Javascript XML节点:添加具有命名空间的属性,javascript,xml,Javascript,Xml,我正在尝试将属性添加到xml节点中。我创建了以下函数 function AddAttribute(xmlNode, attrname, attrvalue, path) { var attr; if (isIE()) attr = xmlNode.ownerDocument.createNode(2, attrname, "http://mydomain/MyNameSpace"); else attr = xmlNode.ownerD

我正在尝试将属性添加到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,效果很好

您知道如何添加名称空间吗? 或者您知道创建具有名称空间的属性的其他替代方法吗


谢谢

我找到了一个可能的解决办法。至少现在它适用于三种浏览器: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”的帮助。

您将什么作为
attrname
传递?我传递:“co:internalcollectiontype”我找到了一个解决方案(可能不是最好的)。我不能发布答案,我需要等8个小时。在此之前,我的评论是:var n=xmlNode.selectSingleNode(path);if(cb.browser.ie)//ie n.setAttributeNode(attr);else n.setAttributeNodeNS(attr);我将在8小时后添加完整的函数:-o.尝试
if(xmlNode.ownerDocument.createAttributeNS)
,这将使您独立于浏览器嗅探。