Context Navigation

Support for programmatically generating markup streams from Python code using
a very simple syntax. The main entry point to this module is the tag object
(which is actually an instance of the ElementFactory class). You should
rarely (if ever) need to directly import and use any of the other classes in
this module.

Elements can be created using the tag object using attribute access. For
example:

This produces an Element instance which can be further modified to add child
nodes and attributes. This is done by "calling" the element: positional
arguments are added as child nodes (alternatively, the Element.append method
can be used for that purpose), whereas keywords arguments are added as
attributes:

As shown above, an Element can easily be directly rendered to XML text by
printing it or using the Python str() function. This is basically a
shortcut for converting the Element to a stream and serializing that
stream:

The tag object also allows creating "fragments", which are basically lists
of nodes (elements or text) that don't have a parent element. This can be useful
for creating snippets of markup that are attached to a parent element later (for
example in a template). Fragments are created by calling the tag object, which
returns an object of type Fragment:

The " character is escaped using a numerical entity.
The order in which attributes are rendered is undefined.

If an attribute value evaluates to None, that attribute is not included
in the output:

>>> print Element('a', name=None)
<a/>

Attribute names that conflict with Python keywords can be specified by
appending an underscore:

>>> print Element('div', class_='warning')
<div class="warning"/>

Nested elements can be added to an element using item access notation.
The call notation can also be used for this and for adding attributes
using keyword arguments, as one would do in the constructor.