2012-02-06 67 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」的幫助。

相關問題