= Generating Markup Programmatically = Markup provides a [wiki:ApiDocs/MarkupBuilder builder] module which lets you generate markup from Python code using a very simple syntax. The main entry point to the `builder` module is the `tag` object (which is actually an instance of the `ElementFactory` class). Elements can be created through this `tag` object using attribute access, for example: {{{ >>> from markup.builder import tag >>> doc = tag.p('Some text and ', tag.a('a link', href='http://example.org/'), '.') >>> doc }}} 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 `append` method can be used for that purpose), whereas keywords arguments are added as attributes: {{{ >>> doc(tag.br) >>> print doc

Some text and a link.

}}} If an attribute name collides with a Python keyword, simply append an underscore to the name: {{{ >>> doc(class_='intro') >>> print doc

Some text and a link.

}}} As shown above, an `Element` can easily be directly rendered to XML text by `print`ing it or using the Python `str()` function. This is basically a shortcut for converting the `Element` to a [wiki:MarkupStream stream] and serializing that stream: {{{ >>> stream = doc.generate() >>> stream >>> print stream

Some text and a link.

}}} The full functionality of streams is available for streams generated this way. For example, sub-streams can be extracted using XPath expressions with the `select()` method: {{{ >>> substream = stream.select('a') >>> substream >>> print substream a link }}}