blob: 0a4038cbef9cb221c11f074babcabcd779a2dfbb [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`xml.sax.saxutils` --- SAX Utilities
2=========================================
3
4.. module:: xml.sax.saxutils
5 :synopsis: Convenience functions and classes for use with SAX.
6.. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no>
7.. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
8
9
Georg Brandl116aa622007-08-15 14:28:22 +000010The module :mod:`xml.sax.saxutils` contains a number of classes and functions
11that are commonly useful when creating SAX applications, either in direct use,
12or as base classes.
13
14
Georg Brandl7f01a132009-09-16 15:58:14 +000015.. function:: escape(data, entities={})
Georg Brandl116aa622007-08-15 14:28:22 +000016
17 Escape ``'&'``, ``'<'``, and ``'>'`` in a string of data.
18
19 You can escape other strings of data by passing a dictionary as the optional
20 *entities* parameter. The keys and values must all be strings; each key will be
Christian Heimesfdab48e2008-01-20 09:06:41 +000021 replaced with its corresponding value. The characters ``'&'``, ``'<'`` and
22 ``'>'`` are always escaped, even if *entities* is provided.
Georg Brandl116aa622007-08-15 14:28:22 +000023
24
Georg Brandl7f01a132009-09-16 15:58:14 +000025.. function:: unescape(data, entities={})
Georg Brandl116aa622007-08-15 14:28:22 +000026
27 Unescape ``'&amp;'``, ``'&lt;'``, and ``'&gt;'`` in a string of data.
28
29 You can unescape other strings of data by passing a dictionary as the optional
30 *entities* parameter. The keys and values must all be strings; each key will be
Christian Heimesfdab48e2008-01-20 09:06:41 +000031 replaced with its corresponding value. ``'&amp'``, ``'&lt;'``, and ``'&gt;'``
32 are always unescaped, even if *entities* is provided.
Georg Brandl116aa622007-08-15 14:28:22 +000033
Georg Brandl116aa622007-08-15 14:28:22 +000034
Georg Brandl7f01a132009-09-16 15:58:14 +000035.. function:: quoteattr(data, entities={})
Georg Brandl116aa622007-08-15 14:28:22 +000036
37 Similar to :func:`escape`, but also prepares *data* to be used as an
38 attribute value. The return value is a quoted version of *data* with any
39 additional required replacements. :func:`quoteattr` will select a quote
40 character based on the content of *data*, attempting to avoid encoding any
41 quote characters in the string. If both single- and double-quote characters
42 are already in *data*, the double-quote characters will be encoded and *data*
43 will be wrapped in double-quotes. The resulting string can be used directly
44 as an attribute value::
45
Collin Winterc79461b2007-09-01 23:34:30 +000046 >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef"))
Georg Brandl116aa622007-08-15 14:28:22 +000047 <element attr="ab ' cd &quot; ef">
48
49 This function is useful when generating attribute values for HTML or any SGML
50 using the reference concrete syntax.
51
Georg Brandl116aa622007-08-15 14:28:22 +000052
R. David Murraya90032a2010-10-17 22:46:45 +000053.. class:: XMLGenerator(out=None, encoding='iso-8859-1', short_empty_elements=False)
Georg Brandl116aa622007-08-15 14:28:22 +000054
55 This class implements the :class:`ContentHandler` interface by writing SAX
56 events back into an XML document. In other words, using an :class:`XMLGenerator`
57 as the content handler will reproduce the original document being parsed. *out*
58 should be a file-like object which will default to *sys.stdout*. *encoding* is
59 the encoding of the output stream which defaults to ``'iso-8859-1'``.
R. David Murraya90032a2010-10-17 22:46:45 +000060 *short_empty_elements* controls the formatting of elements that contain no
61 content: if *False* (the default) they are emitted as a pair of start/end
62 tags, if set to *True* they are emitted as a single self-closed tag.
63
64 .. versionadded:: 3.2
Georg Brandl61063cc2012-06-24 22:48:30 +020065 The *short_empty_elements* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000066
67
68.. class:: XMLFilterBase(base)
69
70 This class is designed to sit between an :class:`XMLReader` and the client
71 application's event handlers. By default, it does nothing but pass requests up
72 to the reader and events on to the handlers unmodified, but subclasses can
73 override specific methods to modify the event stream or the configuration
74 requests as they pass through.
75
76
Georg Brandl7f01a132009-09-16 15:58:14 +000077.. function:: prepare_input_source(source, base='')
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 This function takes an input source and an optional base URL and returns a fully
80 resolved :class:`InputSource` object ready for reading. The input source can be
81 given as a string, a file-like object, or an :class:`InputSource` object;
82 parsers will use this function to implement the polymorphic *source* argument to
83 their :meth:`parse` method.
84