0

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

  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);
} 

This code does not work in Firefox . It adds the node, but it does not add the namespace. I have tried in IE and in Chrome and it works fine.

Do you know how can I add the namespace? Or do you know any other alternative to create an attribute with a namespace?

Thanks

5
  • what do you pass as attrname? Commented Feb 6, 2012 at 13:04
  • I pass: "co:internalcollectiontype" Commented Feb 6, 2012 at 13:06
  • I have found a solution (probably not the best one). I cannot post as an answer, I need to wait 8 hours. until then here is my comment: var n = xmlNode.selectSingleNode(path); if (cb.browser.ie) //IE n.setAttributeNode(attr); else n.setAttributeNodeNS(attr); Commented Feb 6, 2012 at 13:31
  • I will add the full function after 8 hours :-o . Commented Feb 6, 2012 at 13:32
  • 1
    Try if (xmlNode.ownerDocument.createAttributeNS), that would make you independent from browser sniffing. Commented Feb 6, 2012 at 13:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.