blob: 317febb3247eadddd4c7dec23e940919b82e8aa6 [file] [log] [blame]
Fred Drakeeaf57aa2000-11-29 06:10:22 +00001\section{\module{xml.dom} ---
2 The Document Object Model API}
Fred Drake669d36f2000-10-24 02:34:45 +00003
Fred Drakeeaf57aa2000-11-29 06:10:22 +00004\declaremodule{standard}{xml.dom}
5\modulesynopsis{Document Object Model API for Python.}
Fred Drake669d36f2000-10-24 02:34:45 +00006\sectionauthor{Paul Prescod}{paul@prescod.net}
Martin v. Löwis338bcbc2003-04-18 22:04:34 +00007\sectionauthor{Martin v. L\"owis}{martin@v.loewis.de}
Fred Drake669d36f2000-10-24 02:34:45 +00008
9\versionadded{2.0}
10
Fred Drakeeaf57aa2000-11-29 06:10:22 +000011The Document Object Model, or ``DOM,'' is a cross-language API from
12the World Wide Web Consortium (W3C) for accessing and modifying XML
13documents. A DOM implementation presents an XML document as a tree
14structure, or allows client code to build such a structure from
15scratch. It then gives access to the structure through a set of
16objects which provided well-known interfaces.
Fred Drake669d36f2000-10-24 02:34:45 +000017
Fred Drakeeaf57aa2000-11-29 06:10:22 +000018The DOM is extremely useful for random-access applications. SAX only
19allows you a view of one bit of the document at a time. If you are
20looking at one SAX element, you have no access to another. If you are
21looking at a text node, you have no access to a containing element.
22When you write a SAX application, you need to keep track of your
23program's position in the document somewhere in your own code. SAX
24does not do it for you. Also, if you need to look ahead in the XML
25document, you are just out of luck.
Fred Drake669d36f2000-10-24 02:34:45 +000026
27Some applications are simply impossible in an event driven model with
Fred Drakeeaf57aa2000-11-29 06:10:22 +000028no access to a tree. Of course you could build some sort of tree
Fred Drake669d36f2000-10-24 02:34:45 +000029yourself in SAX events, but the DOM allows you to avoid writing that
Fred Drakeeaf57aa2000-11-29 06:10:22 +000030code. The DOM is a standard tree representation for XML data.
Fred Drake669d36f2000-10-24 02:34:45 +000031
Fred Drakeeaf57aa2000-11-29 06:10:22 +000032%What if your needs are somewhere between SAX and the DOM? Perhaps
33%you cannot afford to load the entire tree in memory but you find the
34%SAX model somewhat cumbersome and low-level. There is also a module
35%called xml.dom.pulldom that allows you to build trees of only the
36%parts of a document that you need structured access to. It also has
37%features that allow you to find your way around the DOM.
Fred Drake669d36f2000-10-24 02:34:45 +000038% See http://www.prescod.net/python/pulldom
39
Fred Drakeeaf57aa2000-11-29 06:10:22 +000040The Document Object Model is being defined by the W3C in stages, or
41``levels'' in their terminology. The Python mapping of the API is
Fred Drake73e8ebf2002-09-11 22:03:47 +000042substantially based on the DOM Level~2 recommendation. The mapping of
43the Level~3 specification, currently only available in draft form, is
44being developed by the \ulink{Python XML Special Interest
45Group}{http://www.python.org/sigs/xml-sig/} as part of the
46\ulink{PyXML package}{http://pyxml.sourceforge.net/}. Refer to the
47documentation bundled with that package for information on the current
48state of DOM Level~3 support.
Fred Drake669d36f2000-10-24 02:34:45 +000049
Fred Drakeeaf57aa2000-11-29 06:10:22 +000050DOM applications typically start by parsing some XML into a DOM. How
Fred Drake73e8ebf2002-09-11 22:03:47 +000051this is accomplished is not covered at all by DOM Level~1, and Level~2
Martin v. Löwis504bc4f2002-09-11 16:26:03 +000052provides only limited improvements: There is a
Fred Drakeeaf57aa2000-11-29 06:10:22 +000053\class{DOMImplementation} object class which provides access to
Martin v. Löwis504bc4f2002-09-11 16:26:03 +000054\class{Document} creation methods, but no way to access an XML
55reader/parser/Document builder in an implementation-independent way.
56There is also no well-defined way to access these methods without an
57existing \class{Document} object. In Python, each DOM implementation
Fred Drake73e8ebf2002-09-11 22:03:47 +000058will provide a function \function{getDOMImplementation()}. DOM Level~3
Martin v. Löwis504bc4f2002-09-11 16:26:03 +000059adds a Load/Store specification, which defines an interface to the
Fred Drake73e8ebf2002-09-11 22:03:47 +000060reader, but this is not yet available in the Python standard library.
Fred Drake669d36f2000-10-24 02:34:45 +000061
62Once you have a DOM document object, you can access the parts of your
63XML document through its properties and methods. These properties are
Fred Drakeeaf57aa2000-11-29 06:10:22 +000064defined in the DOM specification; this portion of the reference manual
65describes the interpretation of the specification in Python.
Fred Drake669d36f2000-10-24 02:34:45 +000066
Fred Drakeeaf57aa2000-11-29 06:10:22 +000067The specification provided by the W3C defines the DOM API for Java,
68ECMAScript, and OMG IDL. The Python mapping defined here is based in
69large part on the IDL version of the specification, but strict
70compliance is not required (though implementations are free to support
71the strict mapping from IDL). See section \ref{dom-conformance},
72``Conformance,'' for a detailed discussion of mapping requirements.
Fred Drake669d36f2000-10-24 02:34:45 +000073
Fred Drake669d36f2000-10-24 02:34:45 +000074
75\begin{seealso}
Fred Drakeeaf57aa2000-11-29 06:10:22 +000076 \seetitle[http://www.w3.org/TR/DOM-Level-2-Core/]{Document Object
Fred Drake73e8ebf2002-09-11 22:03:47 +000077 Model (DOM) Level~2 Specification}
Fred Drakeeaf57aa2000-11-29 06:10:22 +000078 {The W3C recommendation upon which the Python DOM API is
79 based.}
80 \seetitle[http://www.w3.org/TR/REC-DOM-Level-1/]{Document Object
Fred Drake73e8ebf2002-09-11 22:03:47 +000081 Model (DOM) Level~1 Specification}
Fred Drakeeaf57aa2000-11-29 06:10:22 +000082 {The W3C recommendation for the
Fred Drake669d36f2000-10-24 02:34:45 +000083 DOM supported by \module{xml.dom.minidom}.}
84 \seetitle[http://pyxml.sourceforge.net]{PyXML}{Users that require a
85 full-featured implementation of DOM should use the PyXML
86 package.}
Fred Drakeeaf57aa2000-11-29 06:10:22 +000087 \seetitle[http://cgi.omg.org/cgi-bin/doc?orbos/99-08-02.pdf]{CORBA
88 Scripting with Python}
89 {This specifies the mapping from OMG IDL to Python.}
Fred Drake669d36f2000-10-24 02:34:45 +000090\end{seealso}
91
Martin v. Löwis7edbd4f2001-02-22 14:05:50 +000092\subsection{Module Contents}
93
94The \module{xml.dom} contains the following functions:
95
96\begin{funcdesc}{registerDOMImplementation}{name, factory}
Fred Drake07e6c502001-02-23 19:15:56 +000097Register the \var{factory} function with the name \var{name}. The
98factory function should return an object which implements the
99\class{DOMImplementation} interface. The factory function can return
100the same object every time, or a new one for each call, as appropriate
101for the specific implementation (e.g. if that implementation supports
Martin v. Löwis7edbd4f2001-02-22 14:05:50 +0000102some customization).
103\end{funcdesc}
104
Fred Drake89d63cc2001-11-30 16:58:15 +0000105\begin{funcdesc}{getDOMImplementation}{\optional{name\optional{, features}}}
Martin v. Löwis7edbd4f2001-02-22 14:05:50 +0000106Return a suitable DOM implementation. The \var{name} is either
107well-known, the module name of a DOM implementation, or
Fred Drake89d63cc2001-11-30 16:58:15 +0000108\code{None}. If it is not \code{None}, imports the corresponding
109module and returns a \class{DOMImplementation} object if the import
110succeeds. If no name is given, and if the environment variable
111\envvar{PYTHON_DOM} is set, this variable is used to find the
112implementation.
Martin v. Löwis7edbd4f2001-02-22 14:05:50 +0000113
Fred Drake89d63cc2001-11-30 16:58:15 +0000114If name is not given, this examines the available implementations to
115find one with the required feature set. If no implementation can be
116found, raise an \exception{ImportError}. The features list must be a
117sequence of \code{(\var{feature}, \var{version})} pairs which are
118passed to the \method{hasFeature()} method on available
119\class{DOMImplementation} objects.
Martin v. Löwis7edbd4f2001-02-22 14:05:50 +0000120\end{funcdesc}
121
Fred Drakebd34b6b2001-11-30 15:37:33 +0000122
123Some convenience constants are also provided:
124
125\begin{datadesc}{EMPTY_NAMESPACE}
126 The value used to indicate that no namespace is associated with a
127 node in the DOM. This is typically found as the
128 \member{namespaceURI} of a node, or used as the \var{namespaceURI}
129 parameter to a namespaces-specific method.
130 \versionadded{2.2}
131\end{datadesc}
132
133\begin{datadesc}{XML_NAMESPACE}
134 The namespace URI associated with the reserved prefix \code{xml}, as
135 defined by
136 \citetitle[http://www.w3.org/TR/REC-xml-names/]{Namespaces in XML}
137 (section~4).
138 \versionadded{2.2}
139\end{datadesc}
140
141\begin{datadesc}{XMLNS_NAMESPACE}
142 The namespace URI for namespace declarations, as defined by
143 \citetitle[http://www.w3.org/TR/DOM-Level-2-Core/core.html]{Document
Fred Drake73e8ebf2002-09-11 22:03:47 +0000144 Object Model (DOM) Level~2 Core Specification} (section~1.1.8).
Fred Drakebd34b6b2001-11-30 15:37:33 +0000145 \versionadded{2.2}
146\end{datadesc}
147
148\begin{datadesc}{XHTML_NAMESPACE}
149 The URI of the XHTML namespace as defined by
150 \citetitle[http://www.w3.org/TR/xhtml1/]{XHTML 1.0: The Extensible
151 HyperText Markup Language} (section~3.1.1).
152 \versionadded{2.2}
153\end{datadesc}
154
155
Martin v. Löwis7edbd4f2001-02-22 14:05:50 +0000156% Should the Node documentation go here?
157
Fred Drakee21e2bb2001-10-26 20:09:49 +0000158In addition, \module{xml.dom} contains a base \class{Node} class and
159the DOM exception classes. The \class{Node} class provided by this
160module does not implement any of the methods or attributes defined by
161the DOM specification; concrete DOM implementations must provide
162those. The \class{Node} class provided as part of this module does
163provide the constants used for the \member{nodeType} attribute on
164concrete \class{Node} objects; they are located within the class
165rather than at the module level to conform with the DOM
166specifications.
Fred Drake669d36f2000-10-24 02:34:45 +0000167
Fred Drakebd34b6b2001-11-30 15:37:33 +0000168
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000169\subsection{Objects in the DOM \label{dom-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000170
171The definitive documentation for the DOM is the DOM specification from
Fred Drake16942f22000-12-07 04:47:51 +0000172the W3C.
Fred Drake669d36f2000-10-24 02:34:45 +0000173
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000174Note that DOM attributes may also be manipulated as nodes instead of
175as simple strings. It is fairly rare that you must do this, however,
176so this usage is not yet documented.
177
178
179\begin{tableiii}{l|l|l}{class}{Interface}{Section}{Purpose}
Fred Drake16942f22000-12-07 04:47:51 +0000180 \lineiii{DOMImplementation}{\ref{dom-implementation-objects}}
181 {Interface to the underlying implementation.}
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000182 \lineiii{Node}{\ref{dom-node-objects}}
183 {Base interface for most objects in a document.}
Fred Drake16942f22000-12-07 04:47:51 +0000184 \lineiii{NodeList}{\ref{dom-nodelist-objects}}
185 {Interface for a sequence of nodes.}
186 \lineiii{DocumentType}{\ref{dom-documenttype-objects}}
187 {Information about the declarations needed to process a document.}
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000188 \lineiii{Document}{\ref{dom-document-objects}}
189 {Object which represents an entire document.}
190 \lineiii{Element}{\ref{dom-element-objects}}
191 {Element nodes in the document hierarchy.}
192 \lineiii{Attr}{\ref{dom-attr-objects}}
193 {Attribute value nodes on element nodes.}
194 \lineiii{Comment}{\ref{dom-comment-objects}}
195 {Representation of comments in the source document.}
196 \lineiii{Text}{\ref{dom-text-objects}}
197 {Nodes containing textual content from the document.}
198 \lineiii{ProcessingInstruction}{\ref{dom-pi-objects}}
199 {Processing instruction representation.}
200\end{tableiii}
201
Fred Drakebc9c1b12000-12-13 17:38:02 +0000202An additional section describes the exceptions defined for working
203with the DOM in Python.
204
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000205
Fred Drake16942f22000-12-07 04:47:51 +0000206\subsubsection{DOMImplementation Objects
207 \label{dom-implementation-objects}}
208
209The \class{DOMImplementation} interface provides a way for
210applications to determine the availability of particular features in
Fred Drake73e8ebf2002-09-11 22:03:47 +0000211the DOM they are using. DOM Level~2 added the ability to create new
Fred Drake16942f22000-12-07 04:47:51 +0000212\class{Document} and \class{DocumentType} objects using the
213\class{DOMImplementation} as well.
214
215\begin{methoddesc}[DOMImplementation]{hasFeature}{feature, version}
216\end{methoddesc}
217
218
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000219\subsubsection{Node Objects \label{dom-node-objects}}
220
Fred Drake669d36f2000-10-24 02:34:45 +0000221All of the components of an XML document are subclasses of
222\class{Node}.
223
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000224\begin{memberdesc}[Node]{nodeType}
Fred Drake669d36f2000-10-24 02:34:45 +0000225An integer representing the node type. Symbolic constants for the
Fred Drake66f98b42001-01-26 20:51:32 +0000226types are on the \class{Node} object:
Fred Drake669d36f2000-10-24 02:34:45 +0000227\constant{ELEMENT_NODE}, \constant{ATTRIBUTE_NODE},
228\constant{TEXT_NODE}, \constant{CDATA_SECTION_NODE},
229\constant{ENTITY_NODE}, \constant{PROCESSING_INSTRUCTION_NODE},
230\constant{COMMENT_NODE}, \constant{DOCUMENT_NODE},
231\constant{DOCUMENT_TYPE_NODE}, \constant{NOTATION_NODE}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000232This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000233\end{memberdesc}
234
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000235\begin{memberdesc}[Node]{parentNode}
Fred Drake16942f22000-12-07 04:47:51 +0000236The parent of the current node, or \code{None} for the document node.
237The value is always a \class{Node} object or \code{None}. For
238\class{Element} nodes, this will be the parent element, except for the
239root element, in which case it will be the \class{Document} object.
240For \class{Attr} nodes, this is always \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000241This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000242\end{memberdesc}
243
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000244\begin{memberdesc}[Node]{attributes}
Fred Drake61f79492001-10-25 20:42:57 +0000245A \class{NamedNodeMap} of attribute objects. Only elements have
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000246actual values for this; others provide \code{None} for this attribute.
Fred Drake9a29dd62000-12-08 06:54:51 +0000247This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000248\end{memberdesc}
249
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000250\begin{memberdesc}[Node]{previousSibling}
Fred Drake669d36f2000-10-24 02:34:45 +0000251The node that immediately precedes this one with the same parent. For
252instance the element with an end-tag that comes just before the
253\var{self} element's start-tag. Of course, XML documents are made
254up of more than just elements so the previous sibling could be text, a
Fred Drake16942f22000-12-07 04:47:51 +0000255comment, or something else. If this node is the first child of the
256parent, this attribute will be \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000257This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000258\end{memberdesc}
259
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000260\begin{memberdesc}[Node]{nextSibling}
Fred Drake669d36f2000-10-24 02:34:45 +0000261The node that immediately follows this one with the same parent. See
Fred Drake16942f22000-12-07 04:47:51 +0000262also \member{previousSibling}. If this is the last child of the
263parent, this attribute will be \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000264This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000265\end{memberdesc}
266
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000267\begin{memberdesc}[Node]{childNodes}
Fred Drake669d36f2000-10-24 02:34:45 +0000268A list of nodes contained within this node.
Fred Drake9a29dd62000-12-08 06:54:51 +0000269This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000270\end{memberdesc}
271
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000272\begin{memberdesc}[Node]{firstChild}
273The first child of the node, if there are any, or \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000274This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000275\end{memberdesc}
276
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000277\begin{memberdesc}[Node]{lastChild}
278The last child of the node, if there are any, or \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000279This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000280\end{memberdesc}
281
Fred Drake9a29dd62000-12-08 06:54:51 +0000282\begin{memberdesc}[Node]{localName}
283The part of the \member{tagName} following the colon if there is one,
284else the entire \member{tagName}. The value is a string.
285\end{memberdesc}
286
287\begin{memberdesc}[Node]{prefix}
288The part of the \member{tagName} preceding the colon if there is one,
289else the empty string. The value is a string, or \code{None}
290\end{memberdesc}
291
292\begin{memberdesc}[Node]{namespaceURI}
Fred Drake16942f22000-12-07 04:47:51 +0000293The namespace associated with the element name. This will be a
Fred Drake9a29dd62000-12-08 06:54:51 +0000294string or \code{None}. This is a read-only attribute.
Fred Drake16942f22000-12-07 04:47:51 +0000295\end{memberdesc}
296
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000297\begin{memberdesc}[Node]{nodeName}
Fred Drake9a29dd62000-12-08 06:54:51 +0000298This has a different meaning for each node type; see the DOM
299specification for details. You can always get the information you
300would get here from another property such as the \member{tagName}
301property for elements or the \member{name} property for attributes.
302For all node types, the value of this attribute will be either a
303string or \code{None}. This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000304\end{memberdesc}
305
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000306\begin{memberdesc}[Node]{nodeValue}
Fred Drake9a29dd62000-12-08 06:54:51 +0000307This has a different meaning for each node type; see the DOM
308specification for details. The situation is similar to that with
309\member{nodeName}. The value is a string or \code{None}.
Fred Drake669d36f2000-10-24 02:34:45 +0000310\end{memberdesc}
311
Fred Drake9a29dd62000-12-08 06:54:51 +0000312\begin{methoddesc}[Node]{hasAttributes}{}
313Returns true if the node has any attributes.
314\end{methoddesc}
315
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000316\begin{methoddesc}[Node]{hasChildNodes}{}
317Returns true if the node has any child nodes.
Fred Drake669d36f2000-10-24 02:34:45 +0000318\end{methoddesc}
319
Fred Drake40e43bf2001-02-03 01:20:01 +0000320\begin{methoddesc}[Node]{isSameNode}{other}
321Returns true if \var{other} refers to the same node as this node.
322This is especially useful for DOM implementations which use any sort
323of proxy architecture (because more than one object can refer to the
324same node).
Fred Drake15862f52001-02-14 20:39:15 +0000325
Fred Drake73e8ebf2002-09-11 22:03:47 +0000326\begin{notice}
327 This is based on a proposed DOM Level~3 API which is still in the
328 ``working draft'' stage, but this particular interface appears
329 uncontroversial. Changes from the W3C will not necessarily affect
330 this method in the Python DOM interface (though any new W3C API for
331 this would also be supported).
332\end{notice}
Fred Drake40e43bf2001-02-03 01:20:01 +0000333\end{methoddesc}
334
Fred Drake9a29dd62000-12-08 06:54:51 +0000335\begin{methoddesc}[Node]{appendChild}{newChild}
336Add a new child node to this node at the end of the list of children,
337returning \var{newChild}.
338\end{methoddesc}
339
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000340\begin{methoddesc}[Node]{insertBefore}{newChild, refChild}
Fred Drake669d36f2000-10-24 02:34:45 +0000341Insert a new child node before an existing child. It must be the case
342that \var{refChild} is a child of this node; if not,
Johannes Gijsbers4de93742004-11-07 19:55:18 +0000343\exception{ValueError} is raised. \var{newChild} is returned. If
344\var{refChild} is \code{None}, it inserts \var{newChild} at the end of
345the children's list.
Fred Drake669d36f2000-10-24 02:34:45 +0000346\end{methoddesc}
347
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000348\begin{methoddesc}[Node]{removeChild}{oldChild}
Fred Drake669d36f2000-10-24 02:34:45 +0000349Remove a child node. \var{oldChild} must be a child of this node; if
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000350not, \exception{ValueError} is raised. \var{oldChild} is returned on
351success. If \var{oldChild} will not be used further, its
352\method{unlink()} method should be called.
Fred Drake669d36f2000-10-24 02:34:45 +0000353\end{methoddesc}
354
Fred Drake9a29dd62000-12-08 06:54:51 +0000355\begin{methoddesc}[Node]{replaceChild}{newChild, oldChild}
356Replace an existing node with a new node. It must be the case that
357\var{oldChild} is a child of this node; if not,
358\exception{ValueError} is raised.
Fred Drake669d36f2000-10-24 02:34:45 +0000359\end{methoddesc}
360
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000361\begin{methoddesc}[Node]{normalize}{}
362Join adjacent text nodes so that all stretches of text are stored as
363single \class{Text} instances. This simplifies processing text from a
364DOM tree for many applications.
365\versionadded{2.1}
Fred Drake669d36f2000-10-24 02:34:45 +0000366\end{methoddesc}
367
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000368\begin{methoddesc}[Node]{cloneNode}{deep}
369Clone this node. Setting \var{deep} means to clone all child nodes as
Fred Drake16942f22000-12-07 04:47:51 +0000370well. This returns the clone.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000371\end{methoddesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000372
373
Fred Drake16942f22000-12-07 04:47:51 +0000374\subsubsection{NodeList Objects \label{dom-nodelist-objects}}
375
376A \class{NodeList} represents a sequence of nodes. These objects are
377used in two ways in the DOM Core recommendation: the
Fred Drake2c184e72002-11-13 15:56:13 +0000378\class{Element} objects provides one as its list of child nodes, and
Fred Drake16942f22000-12-07 04:47:51 +0000379the \method{getElementsByTagName()} and
380\method{getElementsByTagNameNS()} methods of \class{Node} return
381objects with this interface to represent query results.
382
Fred Drake73e8ebf2002-09-11 22:03:47 +0000383The DOM Level~2 recommendation defines one method and one attribute
Fred Drake16942f22000-12-07 04:47:51 +0000384for these objects:
385
386\begin{methoddesc}[NodeList]{item}{i}
387 Return the \var{i}'th item from the sequence, if there is one, or
388 \code{None}. The index \var{i} is not allowed to be less then zero
389 or greater than or equal to the length of the sequence.
390\end{methoddesc}
391
392\begin{memberdesc}[NodeList]{length}
393 The number of nodes in the sequence.
394\end{memberdesc}
395
396In addition, the Python DOM interface requires that some additional
397support is provided to allow \class{NodeList} objects to be used as
398Python sequences. All \class{NodeList} implementations must include
399support for \method{__len__()} and \method{__getitem__()}; this allows
400iteration over the \class{NodeList} in \keyword{for} statements and
401proper support for the \function{len()} built-in function.
402
403If a DOM implementation supports modification of the document, the
404\class{NodeList} implementation must also support the
405\method{__setitem__()} and \method{__delitem__()} methods.
406
407
408\subsubsection{DocumentType Objects \label{dom-documenttype-objects}}
409
410Information about the notations and entities declared by a document
411(including the external subset if the parser uses it and can provide
412the information) is available from a \class{DocumentType} object. The
413\class{DocumentType} for a document is available from the
Fred Drakee21e2bb2001-10-26 20:09:49 +0000414\class{Document} object's \member{doctype} attribute; if there is no
415\code{DOCTYPE} declaration for the document, the document's
416\member{doctype} attribute will be set to \code{None} instead of an
417instance of this interface.
Fred Drake16942f22000-12-07 04:47:51 +0000418
419\class{DocumentType} is a specialization of \class{Node}, and adds the
420following attributes:
421
422\begin{memberdesc}[DocumentType]{publicId}
423 The public identifier for the external subset of the document type
424 definition. This will be a string or \code{None}.
425\end{memberdesc}
426
427\begin{memberdesc}[DocumentType]{systemId}
428 The system identifier for the external subset of the document type
429 definition. This will be a URI as a string, or \code{None}.
430\end{memberdesc}
431
432\begin{memberdesc}[DocumentType]{internalSubset}
433 A string giving the complete internal subset from the document.
Fred Drakef459d852001-04-05 18:30:04 +0000434 This does not include the brackets which enclose the subset. If the
435 document has no internal subset, this should be \code{None}.
Fred Drake16942f22000-12-07 04:47:51 +0000436\end{memberdesc}
437
438\begin{memberdesc}[DocumentType]{name}
439 The name of the root element as given in the \code{DOCTYPE}
Fred Drakee21e2bb2001-10-26 20:09:49 +0000440 declaration, if present.
Fred Drake16942f22000-12-07 04:47:51 +0000441\end{memberdesc}
442
443\begin{memberdesc}[DocumentType]{entities}
444 This is a \class{NamedNodeMap} giving the definitions of external
445 entities. For entity names defined more than once, only the first
446 definition is provided (others are ignored as required by the XML
447 recommendation). This may be \code{None} if the information is not
448 provided by the parser, or if no entities are defined.
449\end{memberdesc}
450
451\begin{memberdesc}[DocumentType]{notations}
452 This is a \class{NamedNodeMap} giving the definitions of notations.
453 For notation names defined more than once, only the first definition
454 is provided (others are ignored as required by the XML
455 recommendation). This may be \code{None} if the information is not
456 provided by the parser, or if no notations are defined.
457\end{memberdesc}
458
459
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000460\subsubsection{Document Objects \label{dom-document-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000461
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000462A \class{Document} represents an entire XML document, including its
463constituent elements, attributes, processing instructions, comments
464etc. Remeber that it inherits properties from \class{Node}.
465
466\begin{memberdesc}[Document]{documentElement}
Fred Drake669d36f2000-10-24 02:34:45 +0000467The one and only root element of the document.
468\end{memberdesc}
469
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000470\begin{methoddesc}[Document]{createElement}{tagName}
Fred Drake16942f22000-12-07 04:47:51 +0000471Create and return a new element node. The element is not inserted
472into the document when it is created. You need to explicitly insert
473it with one of the other methods such as \method{insertBefore()} or
Fred Drake669d36f2000-10-24 02:34:45 +0000474\method{appendChild()}.
475\end{methoddesc}
476
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000477\begin{methoddesc}[Document]{createElementNS}{namespaceURI, tagName}
Fred Drake16942f22000-12-07 04:47:51 +0000478Create and return a new element with a namespace. The
479\var{tagName} may have a prefix. The element is not inserted into the
480document when it is created. You need to explicitly insert it with
481one of the other methods such as \method{insertBefore()} or
482\method{appendChild()}.
Fred Drake669d36f2000-10-24 02:34:45 +0000483\end{methoddesc}
484
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000485\begin{methoddesc}[Document]{createTextNode}{data}
Fred Drake16942f22000-12-07 04:47:51 +0000486Create and return a text node containing the data passed as a
487parameter. As with the other creation methods, this one does not
488insert the node into the tree.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000489\end{methoddesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000490
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000491\begin{methoddesc}[Document]{createComment}{data}
Fred Drake16942f22000-12-07 04:47:51 +0000492Create and return a comment node containing the data passed as a
493parameter. As with the other creation methods, this one does not
494insert the node into the tree.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000495\end{methoddesc}
496
497\begin{methoddesc}[Document]{createProcessingInstruction}{target, data}
Fred Drake16942f22000-12-07 04:47:51 +0000498Create and return a processing instruction node containing the
499\var{target} and \var{data} passed as parameters. As with the other
500creation methods, this one does not insert the node into the tree.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000501\end{methoddesc}
502
503\begin{methoddesc}[Document]{createAttribute}{name}
Fred Drake16942f22000-12-07 04:47:51 +0000504Create and return an attribute node. This method does not associate
505the attribute node with any particular element. You must use
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000506\method{setAttributeNode()} on the appropriate \class{Element} object
507to use the newly created attribute instance.
508\end{methoddesc}
509
510\begin{methoddesc}[Document]{createAttributeNS}{namespaceURI, qualifiedName}
Fred Drake16942f22000-12-07 04:47:51 +0000511Create and return an attribute node with a namespace. The
512\var{tagName} may have a prefix. This method does not associate the
513attribute node with any particular element. You must use
514\method{setAttributeNode()} on the appropriate \class{Element} object
515to use the newly created attribute instance.
Fred Drake669d36f2000-10-24 02:34:45 +0000516\end{methoddesc}
517
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000518\begin{methoddesc}[Document]{getElementsByTagName}{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000519Search for all descendants (direct children, children's children,
520etc.) with a particular element type name.
521\end{methoddesc}
522
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000523\begin{methoddesc}[Document]{getElementsByTagNameNS}{namespaceURI, localName}
Fred Drake669d36f2000-10-24 02:34:45 +0000524Search for all descendants (direct children, children's children,
525etc.) with a particular namespace URI and localname. The localname is
526the part of the namespace after the prefix.
527\end{methoddesc}
528
Fred Drake669d36f2000-10-24 02:34:45 +0000529
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000530\subsubsection{Element Objects \label{dom-element-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000531
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000532\class{Element} is a subclass of \class{Node}, so inherits all the
533attributes of that class.
534
535\begin{memberdesc}[Element]{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000536The element type name. In a namespace-using document it may have
Fred Drake16942f22000-12-07 04:47:51 +0000537colons in it. The value is a string.
Fred Drake669d36f2000-10-24 02:34:45 +0000538\end{memberdesc}
539
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000540\begin{methoddesc}[Element]{getElementsByTagName}{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000541Same as equivalent method in the \class{Document} class.
542\end{methoddesc}
543
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000544\begin{methoddesc}[Element]{getElementsByTagNameNS}{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000545Same as equivalent method in the \class{Document} class.
546\end{methoddesc}
547
Fred Drake9a29dd62000-12-08 06:54:51 +0000548\begin{methoddesc}[Element]{getAttribute}{attname}
549Return an attribute value as a string.
550\end{methoddesc}
551
552\begin{methoddesc}[Element]{getAttributeNode}{attrname}
553Return the \class{Attr} node for the attribute named by
554\var{attrname}.
555\end{methoddesc}
556
557\begin{methoddesc}[Element]{getAttributeNS}{namespaceURI, localName}
558Return an attribute value as a string, given a \var{namespaceURI} and
559\var{localName}.
560\end{methoddesc}
561
562\begin{methoddesc}[Element]{getAttributeNodeNS}{namespaceURI, localName}
563Return an attribute value as a node, given a \var{namespaceURI} and
564\var{localName}.
565\end{methoddesc}
566
567\begin{methoddesc}[Element]{removeAttribute}{attname}
568Remove an attribute by name. No exception is raised if there is no
569matching attribute.
570\end{methoddesc}
571
572\begin{methoddesc}[Element]{removeAttributeNode}{oldAttr}
573Remove and return \var{oldAttr} from the attribute list, if present.
574If \var{oldAttr} is not present, \exception{NotFoundErr} is raised.
575\end{methoddesc}
576
577\begin{methoddesc}[Element]{removeAttributeNS}{namespaceURI, localName}
578Remove an attribute by name. Note that it uses a localName, not a
579qname. No exception is raised if there is no matching attribute.
580\end{methoddesc}
581
582\begin{methoddesc}[Element]{setAttribute}{attname, value}
583Set an attribute value from a string.
584\end{methoddesc}
585
586\begin{methoddesc}[Element]{setAttributeNode}{newAttr}
587Add a new attibute node to the element, replacing an existing
588attribute if necessary if the \member{name} attribute matches. If a
589replacement occurs, the old attribute node will be returned. If
590\var{newAttr} is already in use, \exception{InuseAttributeErr} will be
591raised.
592\end{methoddesc}
593
594\begin{methoddesc}[Element]{setAttributeNodeNS}{newAttr}
595Add a new attibute node to the element, replacing an existing
596attribute if necessary if the \member{namespaceURI} and
597\member{localName} attributes match. If a replacement occurs, the old
598attribute node will be returned. If \var{newAttr} is already in use,
599\exception{InuseAttributeErr} will be raised.
600\end{methoddesc}
601
602\begin{methoddesc}[Element]{setAttributeNS}{namespaceURI, qname, value}
603Set an attribute value from a string, given a \var{namespaceURI} and a
604\var{qname}. Note that a qname is the whole attribute name. This is
605different than above.
606\end{methoddesc}
607
Fred Drake669d36f2000-10-24 02:34:45 +0000608
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000609\subsubsection{Attr Objects \label{dom-attr-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000610
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000611\class{Attr} inherits from \class{Node}, so inherits all its
612attributes.
Fred Drake669d36f2000-10-24 02:34:45 +0000613
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000614\begin{memberdesc}[Attr]{name}
Fred Drake669d36f2000-10-24 02:34:45 +0000615The attribute name. In a namespace-using document it may have colons
616in it.
617\end{memberdesc}
618
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000619\begin{memberdesc}[Attr]{localName}
Fred Drake669d36f2000-10-24 02:34:45 +0000620The part of the name following the colon if there is one, else the
Fred Drake9a29dd62000-12-08 06:54:51 +0000621entire name. This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000622\end{memberdesc}
623
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000624\begin{memberdesc}[Attr]{prefix}
Fred Drake669d36f2000-10-24 02:34:45 +0000625The part of the name preceding the colon if there is one, else the
626empty string.
627\end{memberdesc}
628
Fred Drake669d36f2000-10-24 02:34:45 +0000629
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000630\subsubsection{NamedNodeMap Objects \label{dom-attributelist-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000631
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000632\class{NamedNodeMap} does \emph{not} inherit from \class{Node}.
Fred Drake669d36f2000-10-24 02:34:45 +0000633
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000634\begin{memberdesc}[NamedNodeMap]{length}
Fred Drake669d36f2000-10-24 02:34:45 +0000635The length of the attribute list.
636\end{memberdesc}
637
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000638\begin{methoddesc}[NamedNodeMap]{item}{index}
Fred Drake669d36f2000-10-24 02:34:45 +0000639Return an attribute with a particular index. The order you get the
640attributes in is arbitrary but will be consistent for the life of a
641DOM. Each item is an attribute node. Get its value with the
642\member{value} attribbute.
643\end{methoddesc}
644
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000645There are also experimental methods that give this class more mapping
646behavior. You can use them or you can use the standardized
Fred Drakee21e2bb2001-10-26 20:09:49 +0000647\method{getAttribute*()} family of methods on the \class{Element}
648objects.
Fred Drake669d36f2000-10-24 02:34:45 +0000649
650
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000651\subsubsection{Comment Objects \label{dom-comment-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000652
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000653\class{Comment} represents a comment in the XML document. It is a
Fred Drake9a29dd62000-12-08 06:54:51 +0000654subclass of \class{Node}, but cannot have child nodes.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000655
656\begin{memberdesc}[Comment]{data}
Fred Drake9a29dd62000-12-08 06:54:51 +0000657The content of the comment as a string. The attribute contains all
658characters between the leading \code{<!-}\code{-} and trailing
659\code{-}\code{->}, but does not include them.
Fred Drake669d36f2000-10-24 02:34:45 +0000660\end{memberdesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000661
662
Fred Drake9a29dd62000-12-08 06:54:51 +0000663\subsubsection{Text and CDATASection Objects \label{dom-text-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000664
Fred Drake9a29dd62000-12-08 06:54:51 +0000665The \class{Text} interface represents text in the XML document. If
666the parser and DOM implementation support the DOM's XML extension,
667portions of the text enclosed in CDATA marked sections are stored in
668\class{CDATASection} objects. These two interfaces are identical, but
669provide different values for the \member{nodeType} attribute.
670
671These interfaces extend the \class{Node} interface. They cannot have
672child nodes.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000673
674\begin{memberdesc}[Text]{data}
Fred Drake9a29dd62000-12-08 06:54:51 +0000675The content of the text node as a string.
Fred Drake669d36f2000-10-24 02:34:45 +0000676\end{memberdesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000677
Fred Drake73e8ebf2002-09-11 22:03:47 +0000678\begin{notice}
679 The use of a \class{CDATASection} node does not indicate that the
680 node represents a complete CDATA marked section, only that the
681 content of the node was part of a CDATA section. A single CDATA
682 section may be represented by more than one node in the document
683 tree. There is no way to determine whether two adjacent
684 \class{CDATASection} nodes represent different CDATA marked
685 sections.
686\end{notice}
Fred Drake9a29dd62000-12-08 06:54:51 +0000687
Fred Drake669d36f2000-10-24 02:34:45 +0000688
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000689\subsubsection{ProcessingInstruction Objects \label{dom-pi-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000690
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000691Represents a processing instruction in the XML document; this inherits
Fred Drake9a29dd62000-12-08 06:54:51 +0000692from the \class{Node} interface and cannot have child nodes.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000693
694\begin{memberdesc}[ProcessingInstruction]{target}
Fred Drake669d36f2000-10-24 02:34:45 +0000695The content of the processing instruction up to the first whitespace
Fred Drake9a29dd62000-12-08 06:54:51 +0000696character. This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000697\end{memberdesc}
698
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000699\begin{memberdesc}[ProcessingInstruction]{data}
Fred Drake669d36f2000-10-24 02:34:45 +0000700The content of the processing instruction following the first
701whitespace character.
702\end{memberdesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000703
704
Fred Drakebc9c1b12000-12-13 17:38:02 +0000705\subsubsection{Exceptions \label{dom-exceptions}}
706
707\versionadded{2.1}
708
Fred Drake73e8ebf2002-09-11 22:03:47 +0000709The DOM Level~2 recommendation defines a single exception,
Fred Drakebc9c1b12000-12-13 17:38:02 +0000710\exception{DOMException}, and a number of constants that allow
711applications to determine what sort of error occurred.
712\exception{DOMException} instances carry a \member{code} attribute
713that provides the appropriate value for the specific exception.
714
715The Python DOM interface provides the constants, but also expands the
716set of exceptions so that a specific exception exists for each of the
717exception codes defined by the DOM. The implementations must raise
718the appropriate specific exception, each of which carries the
719appropriate value for the \member{code} attribute.
720
721\begin{excdesc}{DOMException}
722 Base exception class used for all specific DOM exceptions. This
723 exception class cannot be directly instantiated.
724\end{excdesc}
725
726\begin{excdesc}{DomstringSizeErr}
727 Raised when a specified range of text does not fit into a string.
728 This is not known to be used in the Python DOM implementations, but
729 may be received from DOM implementations not written in Python.
730\end{excdesc}
731
732\begin{excdesc}{HierarchyRequestErr}
733 Raised when an attempt is made to insert a node where the node type
734 is not allowed.
735\end{excdesc}
736
737\begin{excdesc}{IndexSizeErr}
738 Raised when an index or size parameter to a method is negative or
739 exceeds the allowed values.
740\end{excdesc}
741
742\begin{excdesc}{InuseAttributeErr}
743 Raised when an attempt is made to insert an \class{Attr} node that
744 is already present elsewhere in the document.
745\end{excdesc}
746
747\begin{excdesc}{InvalidAccessErr}
748 Raised if a parameter or an operation is not supported on the
749 underlying object.
750\end{excdesc}
751
752\begin{excdesc}{InvalidCharacterErr}
753 This exception is raised when a string parameter contains a
754 character that is not permitted in the context it's being used in by
755 the XML 1.0 recommendation. For example, attempting to create an
756 \class{Element} node with a space in the element type name will
757 cause this error to be raised.
758\end{excdesc}
759
760\begin{excdesc}{InvalidModificationErr}
761 Raised when an attempt is made to modify the type of a node.
762\end{excdesc}
763
764\begin{excdesc}{InvalidStateErr}
Brett Cannon37068552004-06-17 21:34:05 +0000765 Raised when an attempt is made to use an object that is not defined or is no
Fred Drakebc9c1b12000-12-13 17:38:02 +0000766 longer usable.
767\end{excdesc}
768
769\begin{excdesc}{NamespaceErr}
770 If an attempt is made to change any object in a way that is not
771 permitted with regard to the
772 \citetitle[http://www.w3.org/TR/REC-xml-names/]{Namespaces in XML}
773 recommendation, this exception is raised.
774\end{excdesc}
775
776\begin{excdesc}{NotFoundErr}
777 Exception when a node does not exist in the referenced context. For
778 example, \method{NamedNodeMap.removeNamedItem()} will raise this if
779 the node passed in does not exist in the map.
780\end{excdesc}
781
782\begin{excdesc}{NotSupportedErr}
783 Raised when the implementation does not support the requested type
784 of object or operation.
785\end{excdesc}
786
787\begin{excdesc}{NoDataAllowedErr}
788 This is raised if data is specified for a node which does not
789 support data.
790 % XXX a better explanation is needed!
791\end{excdesc}
792
793\begin{excdesc}{NoModificationAllowedErr}
794 Raised on attempts to modify an object where modifications are not
795 allowed (such as for read-only nodes).
796\end{excdesc}
797
798\begin{excdesc}{SyntaxErr}
799 Raised when an invalid or illegal string is specified.
800 % XXX how is this different from InvalidCharacterErr ???
801\end{excdesc}
802
803\begin{excdesc}{WrongDocumentErr}
804 Raised when a node is inserted in a different document than it
805 currently belongs to, and the implementation does not support
806 migrating the node from one document to the other.
807\end{excdesc}
808
809The exception codes defined in the DOM recommendation map to the
810exceptions described above according to this table:
811
812\begin{tableii}{l|l}{constant}{Constant}{Exception}
813 \lineii{DOMSTRING_SIZE_ERR}{\exception{DomstringSizeErr}}
814 \lineii{HIERARCHY_REQUEST_ERR}{\exception{HierarchyRequestErr}}
815 \lineii{INDEX_SIZE_ERR}{\exception{IndexSizeErr}}
816 \lineii{INUSE_ATTRIBUTE_ERR}{\exception{InuseAttributeErr}}
817 \lineii{INVALID_ACCESS_ERR}{\exception{InvalidAccessErr}}
818 \lineii{INVALID_CHARACTER_ERR}{\exception{InvalidCharacterErr}}
819 \lineii{INVALID_MODIFICATION_ERR}{\exception{InvalidModificationErr}}
820 \lineii{INVALID_STATE_ERR}{\exception{InvalidStateErr}}
821 \lineii{NAMESPACE_ERR}{\exception{NamespaceErr}}
822 \lineii{NOT_FOUND_ERR}{\exception{NotFoundErr}}
823 \lineii{NOT_SUPPORTED_ERR}{\exception{NotSupportedErr}}
824 \lineii{NO_DATA_ALLOWED_ERR}{\exception{NoDataAllowedErr}}
825 \lineii{NO_MODIFICATION_ALLOWED_ERR}{\exception{NoModificationAllowedErr}}
826 \lineii{SYNTAX_ERR}{\exception{SyntaxErr}}
827 \lineii{WRONG_DOCUMENT_ERR}{\exception{WrongDocumentErr}}
828\end{tableii}
829
830
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000831\subsection{Conformance \label{dom-conformance}}
Fred Drake669d36f2000-10-24 02:34:45 +0000832
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000833This section describes the conformance requirements and relationships
834between the Python DOM API, the W3C DOM recommendations, and the OMG
835IDL mapping for Python.
Fred Drake669d36f2000-10-24 02:34:45 +0000836
Fred Drake16942f22000-12-07 04:47:51 +0000837
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000838\subsubsection{Type Mapping \label{dom-type-mapping}}
Fred Drake669d36f2000-10-24 02:34:45 +0000839
Fred Drake16942f22000-12-07 04:47:51 +0000840The primitive IDL types used in the DOM specification are mapped to
841Python types according to the following table.
842
843\begin{tableii}{l|l}{code}{IDL Type}{Python Type}
844 \lineii{boolean}{\code{IntegerType} (with a value of \code{0} or \code{1})}
845 \lineii{int}{\code{IntegerType}}
846 \lineii{long int}{\code{IntegerType}}
847 \lineii{unsigned int}{\code{IntegerType}}
848\end{tableii}
849
850Additionally, the \class{DOMString} defined in the recommendation is
851mapped to a Python string or Unicode string. Applications should
852be able to handle Unicode whenever a string is returned from the DOM.
853
854The IDL \keyword{null} value is mapped to \code{None}, which may be
855accepted or provided by the implementation whenever \keyword{null} is
856allowed by the API.
857
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000858
859\subsubsection{Accessor Methods \label{dom-accessor-methods}}
860
861The mapping from OMG IDL to Python defines accessor functions for IDL
862\keyword{attribute} declarations in much the way the Java mapping
863does. Mapping the IDL declarations
Fred Drake669d36f2000-10-24 02:34:45 +0000864
865\begin{verbatim}
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000866readonly attribute string someValue;
867 attribute string anotherValue;
Fred Drake669d36f2000-10-24 02:34:45 +0000868\end{verbatim}
869
Andrew M. Kuchlinge7e03cd2001-06-23 16:26:44 +0000870yields three accessor functions: a ``get'' method for
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000871\member{someValue} (\method{_get_someValue()}), and ``get'' and
872``set'' methods for
873\member{anotherValue} (\method{_get_anotherValue()} and
874\method{_set_anotherValue()}). The mapping, in particular, does not
875require that the IDL attributes are accessible as normal Python
876attributes: \code{\var{object}.someValue} is \emph{not} required to
877work, and may raise an \exception{AttributeError}.
Fred Drake669d36f2000-10-24 02:34:45 +0000878
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000879The Python DOM API, however, \emph{does} require that normal attribute
880access work. This means that the typical surrogates generated by
881Python IDL compilers are not likely to work, and wrapper objects may
882be needed on the client if the DOM objects are accessed via CORBA.
883While this does require some additional consideration for CORBA DOM
884clients, the implementers with experience using DOM over CORBA from
885Python do not consider this a problem. Attributes that are declared
886\keyword{readonly} may not restrict write access in all DOM
887implementations.
Fred Drake669d36f2000-10-24 02:34:45 +0000888
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000889Additionally, the accessor functions are not required. If provided,
890they should take the form defined by the Python IDL mapping, but
891these methods are considered unnecessary since the attributes are
Fred Drake16942f22000-12-07 04:47:51 +0000892accessible directly from Python. ``Set'' accessors should never be
893provided for \keyword{readonly} attributes.