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