Skip to content

Commit e48c25e

Browse files
gh-151943: Document more details of the DOM interfaces
Reported in gh-156388: * which node types can have children, and of which types; * the values of nodeName and nodeValue for every node type; * childNodes is a NodeList; * NodeList does not inherit from Node; * when DocumentType.publicId and systemId are None; * "minidom" is the only well-known implementation name; * which mapping methods minidom adds to a NamedNodeMap, and where they are not available.
1 parent 28dcdee commit e48c25e

1 file changed

Lines changed: 71 additions & 16 deletions

File tree

Doc/library/xml.dom.rst

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ The :mod:`!xml.dom` contains the following functions:
9696
the corresponding module and returns a :class:`DOMImplementation` object if the
9797
import succeeds. If no name is given, and if the environment variable
9898
:envvar:`!PYTHON_DOM` is set, this variable is used to find the implementation.
99+
The only well-known name in the standard library is ``'minidom'``,
100+
for :mod:`xml.dom.minidom`.
99101

100102
If name is not given, this examines the available implementations to find one
101103
with the required feature set. If no implementation can be found, raise an
@@ -241,6 +243,23 @@ Node Objects
241243

242244
All of the components of an XML document are subclasses of :class:`Node`.
243245

246+
Only nodes of the following types can have children,
247+
and only children of the listed types:
248+
249+
:class:`Document`
250+
at most one :class:`Element`, at most one :class:`DocumentType`,
251+
:class:`ProcessingInstruction` and :class:`Comment`
252+
253+
:class:`DocumentFragment` and :class:`Element`
254+
:class:`Element`, :class:`Text`, :class:`CDATASection`,
255+
:class:`ProcessingInstruction` and :class:`Comment`
256+
257+
:class:`Attr`
258+
:class:`Text`
259+
260+
Nodes of other types cannot have children.
261+
Inserting a child of a not allowed type raises :exc:`HierarchyRequestErr`.
262+
244263

245264
.. attribute:: Node.nodeType
246265

@@ -301,7 +320,9 @@ All of the components of an XML document are subclasses of :class:`Node`.
301320

302321
.. attribute:: Node.childNodes
303322

304-
A list of nodes contained within this node. This is a read-only attribute.
323+
A :class:`NodeList` of the children of this node.
324+
If the node has no children, the list is empty.
325+
This is a read-only attribute.
305326

306327

307328
.. attribute:: Node.firstChild
@@ -364,20 +385,48 @@ All of the components of an XML document are subclasses of :class:`Node`.
364385

365386
.. attribute:: Node.nodeName
366387

367-
This has a different meaning for each node type; see the DOM specification for
368-
details. You can always get the information you would get here from another
388+
The name of this node, depending on its type; see the table below.
389+
You can always get the information you would get here from another
369390
property such as the :attr:`~Element.tagName` property for elements or the
370-
:attr:`~Attr.name` property for attributes. For all node types, the value of
371-
this attribute will be either a string or ``None``.
391+
:attr:`~Attr.name` property for attributes.
392+
For all node types, the value of this attribute is a string or ``None``.
372393
This is a read-only attribute.
373394

374395

375396
.. attribute:: Node.nodeValue
376397

377-
This has a different meaning for each node type; see the DOM specification for
378-
details. The situation is similar to that with :attr:`nodeName`. The value is
379-
a string or ``None``.
380-
398+
The value of this node, depending on its type; see the table below.
399+
The value is a string or ``None``.
400+
401+
402+
The values of :attr:`~Node.nodeName` and :attr:`~Node.nodeValue`
403+
for each node type are:
404+
405+
+--------------------------------+---------------------------------------+-------------------------------------+
406+
| Node type | nodeName | nodeValue |
407+
+================================+=======================================+=====================================+
408+
| :class:`Attr` | :attr:`~Attr.name` | :attr:`~Attr.value` |
409+
+--------------------------------+---------------------------------------+-------------------------------------+
410+
| :class:`CDATASection` | ``'#cdata-section'`` | the content |
411+
+--------------------------------+---------------------------------------+-------------------------------------+
412+
| :class:`Comment` | ``'#comment'`` | the content |
413+
+--------------------------------+---------------------------------------+-------------------------------------+
414+
| :class:`Document` | ``'#document'`` | ``None`` |
415+
+--------------------------------+---------------------------------------+-------------------------------------+
416+
| :class:`DocumentFragment` | ``'#document-fragment'`` | ``None`` |
417+
+--------------------------------+---------------------------------------+-------------------------------------+
418+
| :class:`DocumentType` | :attr:`~DocumentType.name` | ``None`` |
419+
+--------------------------------+---------------------------------------+-------------------------------------+
420+
| :class:`Element` | :attr:`~Element.tagName` | ``None`` |
421+
+--------------------------------+---------------------------------------+-------------------------------------+
422+
| :class:`Entity` | the name of the entity | ``None`` |
423+
+--------------------------------+---------------------------------------+-------------------------------------+
424+
| :class:`Notation` | the name of the notation | ``None`` |
425+
+--------------------------------+---------------------------------------+-------------------------------------+
426+
| :class:`ProcessingInstruction` | :attr:`~ProcessingInstruction.target` | :attr:`~ProcessingInstruction.data` |
427+
+--------------------------------+---------------------------------------+-------------------------------------+
428+
| :class:`Text` | ``'#text'`` | the content |
429+
+--------------------------------+---------------------------------------+-------------------------------------+
381430

382431
.. method:: Node.hasAttributes()
383432

@@ -459,6 +508,8 @@ one as its list of child nodes, and the :meth:`~Element.getElementsByTagName`
459508
and :meth:`~Element.getElementsByTagNameNS` methods of :class:`Node` return
460509
objects with this interface to represent query results.
461510

511+
:class:`NodeList` does *not* inherit from :class:`Node`.
512+
462513
The DOM Level 2 recommendation defines one method and one attribute for these
463514
objects:
464515

@@ -509,14 +560,14 @@ following attributes:
509560

510561
.. attribute:: DocumentType.publicId
511562

512-
The public identifier for the external subset of the document type definition.
513-
This will be a string or ``None``.
563+
The public identifier for the external subset of the document type definition,
564+
or ``None`` if the ``DOCTYPE`` declaration does not specify it.
514565

515566

516567
.. attribute:: DocumentType.systemId
517568

518-
The system identifier for the external subset of the document type definition.
519-
This will be a URI as a string, or ``None``.
569+
The system identifier, a URI, for the external subset of the document type
570+
definition, or ``None`` if the ``DOCTYPE`` declaration does not specify it.
520571

521572

522573
.. attribute:: DocumentType.internalSubset
@@ -945,9 +996,13 @@ NamedNodeMap Objects
945996
Remove and return the node with the given namespace URI and local name.
946997
Raise :exc:`NotFoundErr` if there is no such node.
947998

948-
There are also experimental methods that give this class more mapping behavior.
949-
You can use them or you can use the standardized :meth:`!getAttribute\*` family
950-
of methods on the :class:`Element` objects.
999+
:mod:`xml.dom.minidom` provides additional methods which make the attribute
1000+
map of an element behave more like a mapping: :meth:`!get`, :meth:`!keys`,
1001+
:meth:`!keysNS`, :meth:`!values`, :meth:`!items` and :meth:`!itemsNS`, as well
1002+
as ``len()``, ``in``, subscription and deletion. They are not available for
1003+
:attr:`DocumentType.entities` and :attr:`DocumentType.notations`, which are
1004+
read-only. You can also use the standardized :meth:`!getAttribute\*` family of
1005+
methods on the :class:`Element` objects.
9511006

9521007

9531008
.. _dom-documentfragment-objects:

0 commit comments

Comments
 (0)