blob: e46fefdf997510d3455dff1e0864931ef2e52518 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no>
8.. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/xml/sax/saxutils.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl116aa622007-08-15 14:28:22 +000014The module :mod:`xml.sax.saxutils` contains a number of classes and functions
15that are commonly useful when creating SAX applications, either in direct use,
16or as base classes.
17
18
Georg Brandl7f01a132009-09-16 15:58:14 +000019.. function:: escape(data, entities={})
Georg Brandl116aa622007-08-15 14:28:22 +000020
21 Escape ``'&'``, ``'<'``, and ``'>'`` in a string of data.
22
23 You can escape other strings of data by passing a dictionary as the optional
24 *entities* parameter. The keys and values must all be strings; each key will be
Christian Heimesfdab48e2008-01-20 09:06:41 +000025 replaced with its corresponding value. The characters ``'&'``, ``'<'`` and
26 ``'>'`` are always escaped, even if *entities* is provided.
Georg Brandl116aa622007-08-15 14:28:22 +000027
28
Georg Brandl7f01a132009-09-16 15:58:14 +000029.. function:: unescape(data, entities={})
Georg Brandl116aa622007-08-15 14:28:22 +000030
31 Unescape ``'&amp;'``, ``'&lt;'``, and ``'&gt;'`` in a string of data.
32
33 You can unescape other strings of data by passing a dictionary as the optional
34 *entities* parameter. The keys and values must all be strings; each key will be
Christian Heimesfdab48e2008-01-20 09:06:41 +000035 replaced with its corresponding value. ``'&amp'``, ``'&lt;'``, and ``'&gt;'``
36 are always unescaped, even if *entities* is provided.
Georg Brandl116aa622007-08-15 14:28:22 +000037
Georg Brandl116aa622007-08-15 14:28:22 +000038
Georg Brandl7f01a132009-09-16 15:58:14 +000039.. function:: quoteattr(data, entities={})
Georg Brandl116aa622007-08-15 14:28:22 +000040
41 Similar to :func:`escape`, but also prepares *data* to be used as an
42 attribute value. The return value is a quoted version of *data* with any
43 additional required replacements. :func:`quoteattr` will select a quote
44 character based on the content of *data*, attempting to avoid encoding any
45 quote characters in the string. If both single- and double-quote characters
46 are already in *data*, the double-quote characters will be encoded and *data*
47 will be wrapped in double-quotes. The resulting string can be used directly
48 as an attribute value::
49
Collin Winterc79461b2007-09-01 23:34:30 +000050 >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef"))
Georg Brandl116aa622007-08-15 14:28:22 +000051 <element attr="ab ' cd &quot; ef">
52
53 This function is useful when generating attribute values for HTML or any SGML
54 using the reference concrete syntax.
55
Georg Brandl116aa622007-08-15 14:28:22 +000056
R. David Murraya90032a2010-10-17 22:46:45 +000057.. class:: XMLGenerator(out=None, encoding='iso-8859-1', short_empty_elements=False)
Georg Brandl116aa622007-08-15 14:28:22 +000058
Serhiy Storchaka15e65902013-08-29 10:28:44 +030059 This class implements the :class:`~xml.sax.handler.ContentHandler` interface
60 by writing SAX
Georg Brandl116aa622007-08-15 14:28:22 +000061 events back into an XML document. In other words, using an :class:`XMLGenerator`
62 as the content handler will reproduce the original document being parsed. *out*
63 should be a file-like object which will default to *sys.stdout*. *encoding* is
64 the encoding of the output stream which defaults to ``'iso-8859-1'``.
R. David Murraya90032a2010-10-17 22:46:45 +000065 *short_empty_elements* controls the formatting of elements that contain no
Serhiy Storchakaa97cd2e2016-10-19 16:43:42 +030066 content: if ``False`` (the default) they are emitted as a pair of start/end
67 tags, if set to ``True`` they are emitted as a single self-closed tag.
R. David Murraya90032a2010-10-17 22:46:45 +000068
69 .. versionadded:: 3.2
Georg Brandl61063cc2012-06-24 22:48:30 +020070 The *short_empty_elements* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000071
72
73.. class:: XMLFilterBase(base)
74
Serhiy Storchaka15e65902013-08-29 10:28:44 +030075 This class is designed to sit between an
76 :class:`~xml.sax.xmlreader.XMLReader` and the client
Georg Brandl116aa622007-08-15 14:28:22 +000077 application's event handlers. By default, it does nothing but pass requests up
78 to the reader and events on to the handlers unmodified, but subclasses can
79 override specific methods to modify the event stream or the configuration
80 requests as they pass through.
81
82
Georg Brandl7f01a132009-09-16 15:58:14 +000083.. function:: prepare_input_source(source, base='')
Georg Brandl116aa622007-08-15 14:28:22 +000084
Serhiy Storchaka15e65902013-08-29 10:28:44 +030085 This function takes an input source and an optional base URL and returns a
86 fully resolved :class:`~xml.sax.xmlreader.InputSource` object ready for
87 reading. The input source can be given as a string, a file-like object, or
88 an :class:`~xml.sax.xmlreader.InputSource` object; parsers will use this
89 function to implement the polymorphic *source* argument to their
90 :meth:`parse` method.
Georg Brandl116aa622007-08-15 14:28:22 +000091