- From: Philippe Le Hegaret <plh@w3.org>
- Date: Fri, 22 Dec 2000 16:42:29 +0100
- To: Garret Wilson <garret@globalmentor.com>
- CC: www-dom list <www-dom@w3.org>
Garret Wilson wrote:
> This worries me a bit regarding how one would use the DOM Level 2 in
> practice. Let's say I have the following document:
>
> <myDocument xmlns="www.me.com" xmlns:myNS="www.me.com" >
> <myElement myAttr="1"/>
> <myElement myNS:myAttr="1"/>
> </myDocument>
>
> Using the DOM Level 2, how can I elegantly get the value of the myElement
> "a" attribute?
Well, given that your document is inelegant, DOM Level 2 will not facilitate
your life. Your two attributes are differents since there are not in the same
namespace. In practice, you'd better use only one form in your document.
> I can't use myElement.getAttributeNS(null, "myAttr"), because
> that would work for the first nested element, not for the second. I can't
> use myElement.getAttributeNS("www.me.com", "myAttr"), because that would
> work for the second element, and not the first. I would instead have to use
> Java code something like the following *every* time I want to get an
> attribute using the DOM Level 2:
>
> public Attr findAttributeNS(Element element, String namespaceURI, String
> localName)
> {
> Attr attr=element.getAttributeNS(namespaceURI, localName);
> if(attr==null)
> {
> if(element.getNamespaceURI().equals(namespaceURI))
> {
> attr=element.getAttributeNS(null, localName);
> }
> }
> return attr;
> }
>
> This to me seems very inelegant and inefficient. Is this how it's supposed
> to work?
Unfortunately yes but again, I would not recommend a such design in the
same namespace. The returned attr will not have the same semantic depending
on if it has a namespace or or. One more little point: findAttributeNS
would better return only namespace aware nodes (to follow the design
of DOM Level 2).
Philippe
Received on Friday, 22 December 2000 10:42:36 UTC