blob: 14cf07839b5dfcf725d3268d67d8034b48a99a7c [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
Serhiy Storchaka15e65902013-08-29 10:28:44 +030055 This class implements the :class:`~xml.sax.handler.ContentHandler` interface
56 by writing SAX
Georg Brandl116aa622007-08-15 14:28:22 +000057 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'``.
R. David Murraya90032a2010-10-17 22:46:45 +000061 *short_empty_elements* controls the formatting of elements that contain no
62 content: if *False* (the default) they are emitted as a pair of start/end
63 tags, if set to *True* they are emitted as a single self-closed tag.
64
65 .. versionadded:: 3.2
Georg Brandl61063cc2012-06-24 22:48:30 +020066 The *short_empty_elements* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000067
68
69.. class:: XMLFilterBase(base)
70
Serhiy Storchaka15e65902013-08-29 10:28:44 +030071 This class is designed to sit between an
72 :class:`~xml.sax.xmlreader.XMLReader` and the client
Georg Brandl116aa622007-08-15 14:28:22 +000073 application's event handlers. By default, it does nothing but pass requests up
74 to the reader and events on to the handlers unmodified, but subclasses can
75 override specific methods to modify the event stream or the configuration
76 requests as they pass through.
77
78
Georg Brandl7f01a132009-09-16 15:58:14 +000079.. function:: prepare_input_source(source, base='')
Georg Brandl116aa622007-08-15 14:28:22 +000080
Serhiy Storchaka15e65902013-08-29 10:28:44 +030081 This function takes an input source and an optional base URL and returns a
82 fully resolved :class:`~xml.sax.xmlreader.InputSource` object ready for
83 reading. The input source can be given as a string, a file-like object, or
84 an :class:`~xml.sax.xmlreader.InputSource` object; parsers will use this
85 function to implement the polymorphic *source* argument to their
86 :meth:`parse` method.
Georg Brandl116aa622007-08-15 14:28:22 +000087