Re: Is appendChild/insertBefore() supposed to raise exceptions for attributes?

On Fri, 22 Dec 2006, Frans Englich wrote:



> * appendChild() always raise an exception when being passed an attribute
> because "they are not actually child nodes of the element". The only way to
> add an Attr node to an element is to use Element::setAttributeNode().
>
> Which one to choose? Or perhaps a third alternative?

It is correct that, as attribute nodes are not child nodes, you should not
be able to insert or append using insert/append methods designed for
child nodes.

"Attr objects inherit the Node interface, but since they are not actually 
child nodes of the element they describe, the DOM does not consider them 
part of the document tree."

"HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow 
children of the type of the newChild node ..."

Of course, whenever you use an absolute word like "only", you just beg to
have me show you how complex DOM is with multiple ways to do anything.
You can always call getAttributes, which returns a NamedNodeMap, which has
its own methods allowing you to add and remove nodes.  So calling
setAttributeNode (or setAttributeNodeNS) is not the only way.

But insertBefore/appendChild does not insert attribute nodes, entity nodes,
notation nodes, document nodes, document fragment nodes, etc. nor does
it allow you to insert an element as a child of a text, CDATASection, PI,
or comment, or anything else not permitted in the definition of the 
hierarchy, however logical it may seem to someone not sticking to the 
definitions.

Ray Whitmer

Received on Friday, 22 December 2006 12:43:09 UTC