Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Element and Attribute Nodes

The Element and Attr node types correspond to the Infoset's element and attribute information items, respectively. The [children] and [parent] Infoset properties of the element information item are exposed via the standard Node methods, and no additional functionality is needed to address these properties. The parent of an Attr node is always null, but the associated Element node is available via the Attr.ownerElement attribute. The [children] property of the attribute information item is available via both Node.nodeValue and via the Text and EntityReference children of the Attr node.

The primary extensions that Element provides are to address the [attributes] Infoset property, which is always treated as distinct from the [children] property. As mentioned previously, the [attributes] property is available via the Node.Attributes read-only attribute. As a convenience, the Element interface also defines Attr-specific methods that mirror the more generic NamedNodeMap methods.

interface Element : Node {
  DOMString getAttributeNS (in DOMString namespaceURI,
                            in DOMString localName);
  Node      getAttributeNodeNS(in DOMString namespaceURI,
                               in DOMString localName);
  void      setAttributeNS(in DOMString namespaceURI,
             in DOMString qualifiedName, in DOMString value)
                                       raises(DOMException);
  Attr      setAttributeNodeNS(in Attr attr)
                                       raises(DOMException);
  void      removeAttributeNS(in DOMString namespaceURI,
                              in DOMString localName)
                                       raises(DOMException);
  Attr      removeAttributeNode(in Attr attr)
                                 raises(DOMException);
   :               :                  :
};

The prenamespace versions of these methods are elided for clarity.

The attribute-specific Element methods are functionally identical to using the NamedNodeMap exposed via the Node.attributes attribute. This simple Java function

import org.w3c.dom.*;
void setIt(Element elem) {
  elem.setAttributeNS("http://x.com/ns", "x:hi", "earth");
}

could be written using generic Node-isms as follows

import org.w3c.dom.*;
void setIt(Node elem) {
  Document doc = elem.getOwnerDocument();
  Node attr = doc.createAttributeNS("http://x.com/ns",
                                    "x:hi");
  Node text = doc.createTextNode ("earth");
  attr.appendChild (text);
  elem.getAttributes().setNamedItemNS(attr);
}

obviously worth the downcast.

Note that the getAttribute methods use an NCName-based local name but the setAttributeNS method uses a QName-based qualified name. This provides control over the namespace prefix that is used to qualify the attribute name. Recall that nonprefixed attribute names are not affiliated with any namespace URI. For that reason, when passing a nonempty namespace URI to setAttributeNS, a namespace prefix must be selected. Additionally, because the DOM does not treat namespace declaration information items as first-class citizens, the burden is on the developer for managing which prefixes are associated with each namespace URI.

There is one other method available on the Element interface that has nothing to do with attributes per se but shares the model of retrieving things by name. This method is getElementsByTagNameNS.

interface Element : Node
  NodeList getElementsByTagNameNS(in DOMString namespaceURI,
                                  in DOMString localname);
    :             :                      :
}

As shown in Figure 2.13, Element.getElementsByTagNameNS returns all descendant elements of the specified name as a NodeList in the order they would have been encountered doing a preorder (left to right, depth first) traversal. Additionally, one can pass the "*" wildcard to either parameter, telling Element.getElementsByTagNameNS to ignore either namespace affiliation or local name (or both). Element.getElementsByTagNameNS is the most sophisticated node retrieval mechanism that is specified by the DOM core. There are separate XML initiatives, such as XPath and XPointer, that define more sophisticated navigation techniques.

Figure 2.13Figure 2.13. Element.gotElementsByTagNameNS in action


  • + Share This
  • 🔖 Save To Your Account