py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Doc / lib / libpyexpat.tex
blob37b36e10a6d72af1d5a27049973d9c732c383820
1 \section{\module{xml.parsers.expat} ---
2 Fast XML parsing using Expat}
4 % Markup notes:
6 % Many of the attributes of the XMLParser objects are callbacks.
7 % Since signature information must be presented, these are described
8 % using the methoddesc environment. Since they are attributes which
9 % are set by client code, in-text references to these attributes
10 % should be marked using the \member macro and should not include the
11 % parentheses used when marking functions and methods.
13 \declaremodule{standard}{xml.parsers.expat}
14 \modulesynopsis{An interface to the Expat non-validating XML parser.}
15 \moduleauthor{Paul Prescod}{paul@prescod.net}
16 \sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
18 \versionadded{2.0}
20 The \module{xml.parsers.expat} module is a Python interface to the
21 Expat\index{Expat} non-validating XML parser.
22 The module provides a single extension type, \class{xmlparser}, that
23 represents the current state of an XML parser. After an
24 \class{xmlparser} object has been created, various attributes of the object
25 can be set to handler functions. When an XML document is then fed to
26 the parser, the handler functions are called for the character data
27 and markup in the XML document.
29 This module uses the \module{pyexpat}\refbimodindex{pyexpat} module to
30 provide access to the Expat parser. Direct use of the
31 \module{pyexpat} module is deprecated.
33 This module provides one exception and one type object:
35 \begin{excdesc}{ExpatError}
36 The exception raised when Expat reports an error.
37 \end{excdesc}
39 \begin{excdesc}{error}
40 Alias for \exception{ExpatError}.
41 \end{excdesc}
43 \begin{datadesc}{XMLParserType}
44 The type of the return values from the \function{ParserCreate()}
45 function.
46 \end{datadesc}
49 The \module{xml.parsers.expat} module contains two functions:
51 \begin{funcdesc}{ErrorString}{errno}
52 Returns an explanatory string for a given error number \var{errno}.
53 \end{funcdesc}
55 \begin{funcdesc}{ParserCreate}{\optional{encoding\optional{,
56 namespace_separator}}}
57 Creates and returns a new \class{xmlparser} object.
58 \var{encoding}, if specified, must be a string naming the encoding
59 used by the XML data. Expat doesn't support as many encodings as
60 Python does, and its repertoire of encodings can't be extended; it
61 supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If
62 \var{encoding} is given it will override the implicit or explicit
63 encoding of the document.
65 Expat can optionally do XML namespace processing for you, enabled by
66 providing a value for \var{namespace_separator}. The value must be a
67 one-character string; a \exception{ValueError} will be raised if the
68 string has an illegal length (\code{None} is considered the same as
69 omission). When namespace processing is enabled, element type names
70 and attribute names that belong to a namespace will be expanded. The
71 element name passed to the element handlers
72 \member{StartElementHandler} and \member{EndElementHandler}
73 will be the concatenation of the namespace URI, the namespace
74 separator character, and the local part of the name. If the namespace
75 separator is a zero byte (\code{chr(0)}) then the namespace URI and
76 the local part will be concatenated without any separator.
78 For example, if \var{namespace_separator} is set to a space character
79 (\character{ }) and the following document is parsed:
81 \begin{verbatim}
82 <?xml version="1.0"?>
83 <root xmlns = "http://default-namespace.org/"
84 xmlns:py = "http://www.python.org/ns/">
85 <py:elem1 />
86 <elem2 xmlns="" />
87 </root>
88 \end{verbatim}
90 \member{StartElementHandler} will receive the following strings
91 for each element:
93 \begin{verbatim}
94 http://default-namespace.org/ root
95 http://www.python.org/ns/ elem1
96 elem2
97 \end{verbatim}
98 \end{funcdesc}
101 \subsection{XMLParser Objects \label{xmlparser-objects}}
103 \class{xmlparser} objects have the following methods:
105 \begin{methoddesc}[xmlparser]{Parse}{data\optional{, isfinal}}
106 Parses the contents of the string \var{data}, calling the appropriate
107 handler functions to process the parsed data. \var{isfinal} must be
108 true on the final call to this method. \var{data} can be the empty
109 string at any time.
110 \end{methoddesc}
112 \begin{methoddesc}[xmlparser]{ParseFile}{file}
113 Parse XML data reading from the object \var{file}. \var{file} only
114 needs to provide the \method{read(\var{nbytes})} method, returning the
115 empty string when there's no more data.
116 \end{methoddesc}
118 \begin{methoddesc}[xmlparser]{SetBase}{base}
119 Sets the base to be used for resolving relative URIs in system
120 identifiers in declarations. Resolving relative identifiers is left
121 to the application: this value will be passed through as the
122 \var{base} argument to the \function{ExternalEntityRefHandler},
123 \function{NotationDeclHandler}, and
124 \function{UnparsedEntityDeclHandler} functions.
125 \end{methoddesc}
127 \begin{methoddesc}[xmlparser]{GetBase}{}
128 Returns a string containing the base set by a previous call to
129 \method{SetBase()}, or \code{None} if
130 \method{SetBase()} hasn't been called.
131 \end{methoddesc}
133 \begin{methoddesc}[xmlparser]{GetInputContext}{}
134 Returns the input data that generated the current event as a string.
135 The data is in the encoding of the entity which contains the text.
136 When called while an event handler is not active, the return value is
137 \code{None}.
138 \versionadded{2.1}
139 \end{methoddesc}
141 \begin{methoddesc}[xmlparser]{ExternalEntityParserCreate}{context\optional{,
142 encoding}}
143 Create a ``child'' parser which can be used to parse an external
144 parsed entity referred to by content parsed by the parent parser. The
145 \var{context} parameter should be the string passed to the
146 \method{ExternalEntityRefHandler()} handler function, described below.
147 The child parser is created with the \member{ordered_attributes},
148 \member{returns_unicode} and \member{specified_attributes} set to the
149 values of this parser.
150 \end{methoddesc}
153 \class{xmlparser} objects have the following attributes:
155 \begin{memberdesc}[xmlparser]{ordered_attributes}
156 Setting this attribute to a non-zero integer causes the attributes to
157 be reported as a list rather than a dictionary. The attributes are
158 presented in the order found in the document text. For each
159 attribute, two list entries are presented: the attribute name and the
160 attribute value. (Older versions of this module also used this
161 format.) By default, this attribute is false; it may be changed at
162 any time.
163 \versionadded{2.1}
164 \end{memberdesc}
166 \begin{memberdesc}[xmlparser]{returns_unicode}
167 If this attribute is set to a non-zero integer, the handler functions
168 will be passed Unicode strings. If \member{returns_unicode} is 0,
169 8-bit strings containing UTF-8 encoded data will be passed to the
170 handlers.
171 \versionchanged[Can be changed at any time to affect the result
172 type.]{1.6}
173 \end{memberdesc}
175 \begin{memberdesc}[xmlparser]{specified_attributes}
176 If set to a non-zero integer, the parser will report only those
177 attributes which were specified in the document instance and not those
178 which were derived from attribute declarations. Applications which
179 set this need to be especially careful to use what additional
180 information is available from the declarations as needed to comply
181 with the standards for the behavior of XML processors. By default,
182 this attribute is false; it may be changed at any time.
183 \versionadded{2.1}
184 \end{memberdesc}
186 The following attributes contain values relating to the most recent
187 error encountered by an \class{xmlparser} object, and will only have
188 correct values once a call to \method{Parse()} or \method{ParseFile()}
189 has raised a \exception{xml.parsers.expat.ExpatError} exception.
191 \begin{memberdesc}[xmlparser]{ErrorByteIndex}
192 Byte index at which an error occurred.
193 \end{memberdesc}
195 \begin{memberdesc}[xmlparser]{ErrorCode}
196 Numeric code specifying the problem. This value can be passed to the
197 \function{ErrorString()} function, or compared to one of the constants
198 defined in the \code{errors} object.
199 \end{memberdesc}
201 \begin{memberdesc}[xmlparser]{ErrorColumnNumber}
202 Column number at which an error occurred.
203 \end{memberdesc}
205 \begin{memberdesc}[xmlparser]{ErrorLineNumber}
206 Line number at which an error occurred.
207 \end{memberdesc}
209 Here is the list of handlers that can be set. To set a handler on an
210 \class{xmlparser} object \var{o}, use
211 \code{\var{o}.\var{handlername} = \var{func}}. \var{handlername} must
212 be taken from the following list, and \var{func} must be a callable
213 object accepting the correct number of arguments. The arguments are
214 all strings, unless otherwise stated.
216 \begin{methoddesc}[xmlparser]{XmlDeclHandler}{version, encoding, standalone}
217 Called when the XML declaration is parsed. The XML declaration is the
218 (optional) declaration of the applicable version of the XML
219 recommendation, the encoding of the document text, and an optional
220 ``standalone'' declaration. \var{version} and \var{encoding} will be
221 strings of the type dictated by the \member{returns_unicode}
222 attribute, and \var{standalone} will be \code{1} if the document is
223 declared standalone, \code{0} if it is declared not to be standalone,
224 or \code{-1} if the standalone clause was omitted.
225 This is only available with Expat version 1.95.0 or newer.
226 \versionadded{2.1}
227 \end{methoddesc}
229 \begin{methoddesc}[xmlparser]{StartDoctypeDeclHandler}{doctypeName,
230 systemId, publicId,
231 has_internal_subset}
232 Called when Expat begins parsing the document type declaration
233 (\code{<!DOCTYPE \ldots}). The \var{doctypeName} is provided exactly
234 as presented. The \var{systemId} and \var{publicId} parameters give
235 the system and public identifiers if specified, or \code{None} if
236 omitted. \var{has_internal_subset} will be true if the document
237 contains and internal document declaration subset.
238 This requires Expat version 1.2 or newer.
239 \end{methoddesc}
241 \begin{methoddesc}[xmlparser]{EndDoctypeDeclHandler}{}
242 Called when Expat is done parsing the document type delaration.
243 This requires Expat version 1.2 or newer.
244 \end{methoddesc}
246 \begin{methoddesc}[xmlparser]{ElementDeclHandler}{name, model}
247 Called once for each element type declaration. \var{name} is the name
248 of the element type, and \var{model} is a representation of the
249 content model.
250 \end{methoddesc}
252 \begin{methoddesc}[xmlparser]{AttlistDeclHandler}{elname, attname,
253 type, default, required}
254 Called for each declared attribute for an element type. If an
255 attribute list declaration declares three attributes, this handler is
256 called three times, once for each attribute. \var{elname} is the name
257 of the element to which the declaration applies and \var{attname} is
258 the name of the attribute declared. The attribute type is a string
259 passed as \var{type}; the possible values are \code{'CDATA'},
260 \code{'ID'}, \code{'IDREF'}, ...
261 \var{default} gives the default value for the attribute used when the
262 attribute is not specified by the document instance, or \code{None} if
263 there is no default value (\code{\#IMPLIED} values). If the attribute
264 is required to be given in the document instance, \var{required} will
265 be true.
266 This requires Expat version 1.95.0 or newer.
267 \end{methoddesc}
269 \begin{methoddesc}[xmlparser]{StartElementHandler}{name, attributes}
270 Called for the start of every element. \var{name} is a string
271 containing the element name, and \var{attributes} is a dictionary
272 mapping attribute names to their values.
273 \end{methoddesc}
275 \begin{methoddesc}[xmlparser]{EndElementHandler}{name}
276 Called for the end of every element.
277 \end{methoddesc}
279 \begin{methoddesc}[xmlparser]{ProcessingInstructionHandler}{target, data}
280 Called for every processing instruction.
281 \end{methoddesc}
283 \begin{methoddesc}[xmlparser]{CharacterDataHandler}{data}
284 Called for character data. This will be called for normal character
285 data, CDATA marked content, and ignorable whitespace. Applications
286 which must distinguish these cases can use the
287 \member{StartCdataSectionHandler}, \member{EndCdataSectionHandler},
288 and \member{ElementDeclHandler} callbacks to collect the required
289 information.
290 \end{methoddesc}
292 \begin{methoddesc}[xmlparser]{UnparsedEntityDeclHandler}{entityName, base,
293 systemId, publicId,
294 notationName}
295 Called for unparsed (NDATA) entity declarations. This is only present
296 for version 1.2 of the Expat library; for more recent versions, use
297 \member{EntityDeclHandler} instead. (The underlying function in the
298 Expat library has been declared obsolete.)
299 \end{methoddesc}
301 \begin{methoddesc}[xmlparser]{EntityDeclHandler}{entityName,
302 is_parameter_entity, value,
303 base, systemId,
304 publicId,
305 notationName}
306 Called for all entity declarations. For parameter and internal
307 entities, \var{value} will be a string giving the declared contents
308 of the entity; this will be \code{None} for external entities. The
309 \var{notationName} parameter will be \code{None} for parsed entities,
310 and the name of the notation for unparsed entities.
311 \var{is_parameter_entity} will be true if the entity is a paremeter
312 entity or false for general entities (most applications only need to
313 be concerned with general entities).
314 This is only available starting with version 1.95.0 of the Expat
315 library.
316 \versionadded{2.1}
317 \end{methoddesc}
319 \begin{methoddesc}[xmlparser]{NotationDeclHandler}{notationName, base,
320 systemId, publicId}
321 Called for notation declarations. \var{notationName}, \var{base}, and
322 \var{systemId}, and \var{publicId} are strings if given. If the
323 public identifier is omitted, \var{publicId} will be \code{None}.
324 \end{methoddesc}
326 \begin{methoddesc}[xmlparser]{StartNamespaceDeclHandler}{prefix, uri}
327 Called when an element contains a namespace declaration. Namespace
328 declarations are processed before the \member{StartElementHandler} is
329 called for the element on which declarations are placed.
330 \end{methoddesc}
332 \begin{methoddesc}[xmlparser]{EndNamespaceDeclHandler}{prefix}
333 Called when the closing tag is reached for an element
334 that contained a namespace declaration. This is called once for each
335 namespace declaration on the element in the reverse of the order for
336 which the \member{StartNamespaceDeclHandler} was called to indicate
337 the start of each namespace declaration's scope. Calls to this
338 handler are made after the corresponding \member{EndElementHandler}
339 for the end of the element.
340 \end{methoddesc}
342 \begin{methoddesc}[xmlparser]{CommentHandler}{data}
343 Called for comments. \var{data} is the text of the comment, excluding
344 the leading `\code{<!-}\code{-}' and trailing `\code{-}\code{->}'.
345 \end{methoddesc}
347 \begin{methoddesc}[xmlparser]{StartCdataSectionHandler}{}
348 Called at the start of a CDATA section. This and
349 \member{StartCdataSectionHandler} are needed to be able to identify
350 the syntactical start and end for CDATA sections.
351 \end{methoddesc}
353 \begin{methoddesc}[xmlparser]{EndCdataSectionHandler}{}
354 Called at the end of a CDATA section.
355 \end{methoddesc}
357 \begin{methoddesc}[xmlparser]{DefaultHandler}{data}
358 Called for any characters in the XML document for
359 which no applicable handler has been specified. This means
360 characters that are part of a construct which could be reported, but
361 for which no handler has been supplied.
362 \end{methoddesc}
364 \begin{methoddesc}[xmlparser]{DefaultHandlerExpand}{data}
365 This is the same as the \function{DefaultHandler},
366 but doesn't inhibit expansion of internal entities.
367 The entity reference will not be passed to the default handler.
368 \end{methoddesc}
370 \begin{methoddesc}[xmlparser]{NotStandaloneHandler}{} Called if the
371 XML document hasn't been declared as being a standalone document.
372 This happens when there is an external subset or a reference to a
373 parameter entity, but the XML declaration does not set standalone to
374 \code{yes} in an XML declaration. If this handler returns \code{0},
375 then the parser will throw an \constant{XML_ERROR_NOT_STANDALONE}
376 error. If this handler is not set, no exception is raised by the
377 parser for this condition.
378 \end{methoddesc}
380 \begin{methoddesc}[xmlparser]{ExternalEntityRefHandler}{context, base,
381 systemId, publicId}
382 Called for references to external entities. \var{base} is the current
383 base, as set by a previous call to \method{SetBase()}. The public and
384 system identifiers, \var{systemId} and \var{publicId}, are strings if
385 given; if the public identifier is not given, \var{publicId} will be
386 \code{None}. The \var{context} value is opaque and should only be
387 used as described below.
389 For external entities to be parsed, this handler must be implemented.
390 It is responsible for creating the sub-parser using
391 \code{ExternalEntityParserCreate(\var{context})}, initializing it with
392 the appropriate callbacks, and parsing the entity. This handler
393 should return an integer; if it returns \code{0}, the parser will
394 throw an \constant{XML_ERROR_EXTERNAL_ENTITY_HANDLING} error,
395 otherwise parsing will continue.
397 If this handler is not provided, external entities are reported by the
398 \member{DefaultHandler} callback, if provided.
399 \end{methoddesc}
402 \subsection{ExpatError Exceptions \label{expaterror-objects}}
403 \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
405 \exception{ExpatError} exceptions have a number of interesting
406 attributes:
408 \begin{memberdesc}[ExpatError]{code}
409 Expat's internal error number for the specific error. This will
410 match one of the constants defined in the \code{errors} object from
411 this module.
412 \versionadded{2.1}
413 \end{memberdesc}
415 \begin{memberdesc}[ExpatError]{lineno}
416 Line number on which the error was detected. The first line is
417 numbered \code{1}.
418 \versionadded{2.1}
419 \end{memberdesc}
421 \begin{memberdesc}[ExpatError]{offset}
422 Character offset into the line where the error occurred. The first
423 column is numbered \code{0}.
424 \versionadded{2.1}
425 \end{memberdesc}
428 \subsection{Example \label{expat-example}}
430 The following program defines three handlers that just print out their
431 arguments.
433 \begin{verbatim}
434 import xml.parsers.expat
436 # 3 handler functions
437 def start_element(name, attrs):
438 print 'Start element:', name, attrs
439 def end_element(name):
440 print 'End element:', name
441 def char_data(data):
442 print 'Character data:', repr(data)
444 p = xml.parsers.expat.ParserCreate()
446 p.StartElementHandler = start_element
447 p.EndElementHandler = end_element
448 p.CharacterDataHandler = char_data
450 p.Parse("""<?xml version="1.0"?>
451 <parent id="top"><child1 name="paul">Text goes here</child1>
452 <child2 name="fred">More text</child2>
453 </parent>""")
454 \end{verbatim}
456 The output from this program is:
458 \begin{verbatim}
459 Start element: parent {'id': 'top'}
460 Start element: child1 {'name': 'paul'}
461 Character data: 'Text goes here'
462 End element: child1
463 Character data: '\n'
464 Start element: child2 {'name': 'fred'}
465 Character data: 'More text'
466 End element: child2
467 Character data: '\n'
468 End element: parent
469 \end{verbatim}
472 \subsection{Content Model Descriptions \label{expat-content-models}}
473 \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
475 Content modules are described using nested tuples. Each tuple
476 contains four values: the type, the quantifier, the name, and a tuple
477 of children. Children are simply additional content module
478 descriptions.
480 The values of the first two fields are constants defined in the
481 \code{model} object of the \module{xml.parsers.expat} module. These
482 constants can be collected in two groups: the model type group and the
483 quantifier group.
485 The constants in the model type group are:
487 \begin{datadescni}{XML_CTYPE_ANY}
488 The element named by the model name was declared to have a content
489 model of \code{ANY}.
490 \end{datadescni}
492 \begin{datadescni}{XML_CTYPE_CHOICE}
493 The named element allows a choice from a number of options; this is
494 used for content models such as \code{(A | B | C)}.
495 \end{datadescni}
497 \begin{datadescni}{XML_CTYPE_EMPTY}
498 Elements which are declared to be \code{EMPTY} have this model type.
499 \end{datadescni}
501 \begin{datadescni}{XML_CTYPE_MIXED}
502 \end{datadescni}
504 \begin{datadescni}{XML_CTYPE_NAME}
505 \end{datadescni}
507 \begin{datadescni}{XML_CTYPE_SEQ}
508 Models which represent a series of models which follow one after the
509 other are indicated with this model type. This is used for models
510 such as \code{(A, B, C)}.
511 \end{datadescni}
514 The constants in the quantifier group are:
516 \begin{datadescni}{XML_CQUANT_NONE}
517 \end{datadescni}
519 \begin{datadescni}{XML_CQUANT_OPT}
520 The model is option: it can appear once or not at all, as for
521 \code{A?}.
522 \end{datadescni}
524 \begin{datadescni}{XML_CQUANT_PLUS}
525 The model must occur one or more times (\code{A+}).
526 \end{datadescni}
528 \begin{datadescni}{XML_CQUANT_REP}
529 The model must occur zero or more times, as for \code{A*}.
530 \end{datadescni}
533 \subsection{Expat error constants \label{expat-errors}}
534 \sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
536 The following constants are provided in the \code{errors} object of
537 the \refmodule{xml.parsers.expat} module. These constants are useful
538 in interpreting some of the attributes of the \exception{ExpatError}
539 exception objects raised when an error has occurred.
541 The \code{errors} object has the following attributes:
543 \begin{datadescni}{XML_ERROR_ASYNC_ENTITY}
544 \end{datadescni}
546 \begin{datadescni}{XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF}
547 An entity reference in an attribute value referred to an external
548 entity instead of an internal entity.
549 \end{datadescni}
551 \begin{datadescni}{XML_ERROR_BAD_CHAR_REF}
552 \end{datadescni}
554 \begin{datadescni}{XML_ERROR_BINARY_ENTITY_REF}
555 \end{datadescni}
557 \begin{datadescni}{XML_ERROR_DUPLICATE_ATTRIBUTE}
558 An attribute was used more than once in a start tag.
559 \end{datadescni}
561 \begin{datadescni}{XML_ERROR_INCORRECT_ENCODING}
562 \end{datadescni}
564 \begin{datadescni}{XML_ERROR_INVALID_TOKEN}
565 \end{datadescni}
567 \begin{datadescni}{XML_ERROR_JUNK_AFTER_DOC_ELEMENT}
568 Something other than whitespace occurred after the document element.
569 \end{datadescni}
571 \begin{datadescni}{XML_ERROR_MISPLACED_XML_PI}
572 \end{datadescni}
574 \begin{datadescni}{XML_ERROR_NO_ELEMENTS}
575 The document contains no elements.
576 \end{datadescni}
578 \begin{datadescni}{XML_ERROR_NO_MEMORY}
579 Expat was not able to allocate memory internally.
580 \end{datadescni}
582 \begin{datadescni}{XML_ERROR_PARAM_ENTITY_REF}
583 \end{datadescni}
585 \begin{datadescni}{XML_ERROR_PARTIAL_CHAR}
586 \end{datadescni}
588 \begin{datadescni}{XML_ERROR_RECURSIVE_ENTITY_REF}
589 \end{datadescni}
591 \begin{datadescni}{XML_ERROR_SYNTAX}
592 Some unspecified syntax error was encountered.
593 \end{datadescni}
595 \begin{datadescni}{XML_ERROR_TAG_MISMATCH}
596 An end tag did not match the innermost open start tag.
597 \end{datadescni}
599 \begin{datadescni}{XML_ERROR_UNCLOSED_TOKEN}
600 \end{datadescni}
602 \begin{datadescni}{XML_ERROR_UNDEFINED_ENTITY}
603 A reference was made to a entity which was not defined.
604 \end{datadescni}
606 \begin{datadescni}{XML_ERROR_UNKNOWN_ENCODING}
607 The document encoding is not supported by Expat.
608 \end{datadescni}