XML node: add an attribute with a namespace

前端 未结 1 908
南旧
南旧 2021-01-28 00:59

I am trying to add an attribute into an xml node. I have created the following function

  function AddAttribute(xmlNode, attrname, attrvalue, path) {
    var att         


        
1条回答
  •  醉话见心
    2021-01-28 01:28

    I have found a possible solution. At least it works now for the three browsers : IE, Firefox and 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);  
    }
    

    Thanks to "Tomalak" for his help.

    0 讨论(0)
提交回复
热议问题