Skip to content

Markup XML

XML parsing and formatting utilities.

XML Module

XML processing utilities.

This submodule provides utilities for escaping, unescaping, and formatting XML content.

Examples

from rite.markup.xml import ( ... xml_escape, ... xml_unescape ... ) xml_escape("value") '<tag>value</tag>'

Modules

xml_escape

XML Escaper

Escape special XML characters.

Examples

from rite.markup.xml import xml_escape xml_escape("value & more") '<tag>value & more</tag>'

Functions

xml_escape(text: str) -> str

Escape special XML characters.

Parameters:

Name Type Description Default
text str

Text to escape.

required

Returns:

Type Description
str

XML-escaped text.

Examples:

>>> xml_escape("5 < 10 & 10 > 5")
'5 &lt; 10 &amp; 10 &gt; 5'
>>> xml_escape('"quoted"')
'&quot;quoted&quot;'
Notes

Escapes: &, <, >, ", ' Uses xml.sax.saxutils.escape.

xml_format

XML Formatter

Format XML with proper indentation.

Examples

from rite.markup.xml import xml_format xml_format("text") # doctest: +SKIP '\n text\n'

Functions

xml_format(xml_string: str, indent: str = ' ') -> str

Format XML string with indentation.

Parameters:

Name Type Description Default
xml_string str

Unformatted XML string.

required
indent str

Indentation string (default: 2 spaces).

' '

Returns:

Type Description
str

Formatted XML string.

Examples:

>>> xml = "<root><child>text</child></root>"
>>> formatted = xml_format(xml)
>>> print(formatted)
<root>
  <child>text</child>
</root>
Notes

Uses xml.dom.minidom for formatting. May fail on malformed XML.

xml_unescape

XML Unescaper

Unescape XML entities.

Examples

from rite.markup.xml import xml_unescape xml_unescape("<tag>value</tag>") 'value'

Functions

xml_unescape(text: str) -> str

Unescape XML entities.

Parameters:

Name Type Description Default
text str

XML-escaped text.

required

Returns:

Type Description
str

Unescaped text.

Examples:

>>> xml_unescape("&lt;root&gt;&lt;/root&gt;")
'<root></root>'
>>> xml_unescape("&amp;&apos;&quot;")
"&'""
Notes

Converts entities like < back to <. Uses xml.sax.saxutils.unescape.

options: show_root_heading: true show_source: false heading_level: 2