Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | The module :mod:`xml.sax.saxutils` contains a number of classes and functions |
| 12 | that are commonly useful when creating SAX applications, either in direct use, |
| 13 | or 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 Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 22 | replaced with its corresponding value. The characters ``'&'``, ``'<'`` and |
| 23 | ``'>'`` are always escaped, even if *entities* is provided. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | .. function:: unescape(data[, entities]) |
| 27 | |
| 28 | Unescape ``'&'``, ``'<'``, and ``'>'`` 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 Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 32 | replaced with its corresponding value. ``'&'``, ``'<'``, and ``'>'`` |
| 33 | are always unescaped, even if *entities* is provided. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
| 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 Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 47 | >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef")) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | <element attr="ab ' cd " ef"> |
| 49 | |
| 50 | This function is useful when generating attribute values for HTML or any SGML |
| 51 | using the reference concrete syntax. |
| 52 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| 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 | |