Monday, June 7, 2010

node.setAttributeNS(namespaceURI, qualifiedName, value)

When trying to implement this missing method in MSXML,
several interesing
inconsistencies in other browsers apeared,
all claim the native support for this method.

Pseudopcode:

set("ns1","a:a","1")
old=getAttributeWithXPath
set("ns1","b:a","2")
new=getAttributeWithXPath
print compare pointers
print new.nodeName
print new.value
print serialized xml


MSXML 6.0

result:false
p2:a
2
xmlns:p2="ns" p2:a="2"


Firefox/3.5.6

result:true
p1:a
2
p2:a="2" xmlns:p2="ns"

Safari/531.22.7

result:true
p1:a
1
p1:a="2" xmlns:p1="ns"


Chrome/5.0.375.55

result:true
p1:a
2
p1:a="2" xmlns:p1="ns"

Of course there is always an excuse:
http://www.w3.org/TR/DOM-Level-2-Core/core.html
Note: DOM Level 1 methods are namespace ignorant. Therefore, while it is safe to use these methods when not dealing with namespaces, using them and the new ones at the same time should be avoided. DOM Level 1 methods solely identify attribute nodes by their nodeName. On the contrary, the DOM Level 2 methods related to namespaces, identify attribute nodes by their namespaceURI and localName. Because of this fundamental difference, mixing both sets of methods can lead to unpredictable results. In particular, using setAttributeNS, an element may have two attributes (or more) that have the same nodeName, but different namespaceURIs. Calling getAttribute with that nodeName could then return any of those attributes. The result depends on the implementation. Similarly, using setAttributeNode, one can set two attributes (or more) that have different nodeNames but the same prefix and namespaceURI. In this case getAttributeNodeNS will return either attribute, in an implementation dependent manner. The only guarantee in such cases is that all methods that access a named item by its nodeName will access the same item, and all methods which access a node by its URI and local name will access the same node. For instance, setAttribute and setAttributeNS affect the node that getAttribute and getAttributeNS, respectively, return.

No comments:

Post a Comment