W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2000

question regarding Element.getAttributeNS()

From: Garret Wilson <garret@globalmentor.com>
Date: Fri, 22 Dec 2000 07:01:31 -0800
Message-ID: <003d01c06c28$12254d20$7245a640@pacbell.net>
To: "www-dom list" <www-dom@w3.org>
Thanks to everyone who replied earlier to clear up that attributes with no
namespace prefix specified will have a null namespace URI under DOM Level 2
(unlike elements, which inherit a namespace URI).

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? 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?

Garret
Received on Friday, 22 December 2000 10:02:07 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:48 GMT