Next / Previous / Contents / TCC Help System / NM Tech homepage

5. The Node class

All the different kinds of nodes in a DOM tree share these attributes from class Node:

.nodeType

The type of the node as an integer constant.

.ELEMENT_NODE, .ATTRIBUTE_NODE, etc.

All nodes have a set of constant attributes that define the .nodeType values for the various node types. Here is a table showing all these constants:

.ATTRIBUTE_NODE Represents an attribute of an XML element.
.CDATA_SECTION_NODE Represents a CDATA section.
.COMMENT_NODE Represents a comment (<!-- ... -->).
.DOCUMENT_FRAGMENT_NODE Represents a fragment of a document.
.DOCUMENT_NODE Represents an entire document.
.DOCUMENT_TYPE_NODE Represents a document type identifier (<!DOCTYPE ... >).
.ELEMENT_NODE Represents an XML element.
.ENTITY_NODE Represents an entity.
.ENTITY_REFERENCE_NODE Represents a reference to an entity (&...;).
.PROCESSING_INSTRUCTION_NODE Represents a processing instruction (<? ... ?>).
.TEXT_NODE Represents some text.
.nodeName

The name of the node.

  • In an element node, this is the element name (e.g., "chapter" for a <chapter>...</chapter> element).

  • For an Attr node, it is the attribute name.

  • For a document type node, it is the name of the document type.

  • For a processing instruction, it is the target name.

.nodeValue

The value of the node.

  • For an attribute node, it is the attribute's value as a string.

  • For a CDATA section, comment, or text node, it is the text inside the CDATA section, comment, or text section.

  • For a processing instruction, it is the content part.

.attributes

Used only for Element nodes, its value is a Python dictionary containing the element's attributes.

In this dictionary, the key of each attribute is a 2-tuple (nsURI, localName), where:

nsURI

is the attribute's namespace URI, or None for the default namespace.

localName

is the attribute's unqualified name.

The corresponding value for that key is the attribute value as a Unicode string.

For example, for an attribute expressed as class='alarm', the key would be (None, u'class') and the value would be u'alarm'.

.childNodes

If the element has children, this attribute is a list of Node objects containing its children in document order.

For Element nodes, this is a list of its child elements; its attributes are not considered children in this sense.

For Document or DocumentFragment nodes, the children might include comments, document types, and processing instructions, as well as an element child that is the root of the XML document.

.firstChild

If the element has children, this attribute will be set to the first child.

.lastChild

If the element has children, this attribute will be the last child.

.localName

For Element or Attr nodes, holds the local part of a fully qualified name. For example, if the element name is "xsl:template', the .localName attribute is "template".

.namespaceURI

For names with a namespace prefix, holds the URI associated with the namespace; otherwise None. Compare the .prefix attribute.

.nextSibling

The next child of the same parent, if any, otherwise None.

.ownerDocument

For every node in the tree, this attribute points at the Document node that roots the tree.

.parentNode

The element's parent node, or None if the element is the root of the tree.

.prefix

The namespace prefix of the element, or None if it doesn't have one. For example, if in this document namespace prefix xsl: is defined by

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

then the .prefix attribute of an xsl:template element's .prefix attribute would be "xsl", and its .namespaceURI attribute would be "http://www.w3.org/1999/XSL/Transform".

.previousSibling

The element's parent's previous child if there is one, otherwise None.

Methods on Node objects include:

5.1. Node.appendChild()

Once you have created a node newChild (e.g., with Section 12.1, “Document.createElementNS()), use this method to attach it to a parent node N:

N.appendChild(newChild)

If N already had some children, the new node will go after the existing children.