Package genshi :: Module builder :: Class Element

Class Element

object --+    
  Fragment --+

Simple XML output generator based on the builder pattern.

Construct XML elements by passing the tag name to the constructor:

>>> print(Element('strong'))

Attributes can be specified using keyword arguments. The values of the arguments will be converted to strings and any special XML characters escaped:

>>> print(Element('textarea', rows=10, cols=60))
<textarea rows="10" cols="60"/>
>>> print(Element('span', title='1 < 2'))
<span title="1 &lt; 2"/>
>>> print(Element('span', title='"baz"'))
<span title="&#34;baz&#34;"/>

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))

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.

>>> print(Element('ul')(Element('li'), Element('li')))
>>> print(Element('a')('Label'))
>>> print(Element('a')('Label', href="target"))
<a href="target">Label</a>

Text nodes can be nested in an element by adding strings instead of elements. Any special characters in the strings are escaped automatically:

>>> print(Element('em')('Hello world'))
<em>Hello world</em>
>>> print(Element('em')(42))
>>> print(Element('em')('1 < 2'))
<em>1 &lt; 2</em>

This technique also allows mixed content:

>>> print(Element('p')('Hello ', Element('b')('world')))
<p>Hello <b>world</b></p>

Quotes are not escaped inside text nodes: >>> print(Element('p')('"Hello"')) <p>"Hello"</p>

Elements can also be combined with other elements or strings using the addition operator, which results in a Fragment object that contains the operands:

>>> print(Element('br') + 'some text' + Element('br'))
<br/>some text<br/>

Elements with a namespace can be generated using the Namespace and/or QName classes:

>>> from genshi.core import Namespace
>>> xhtml = Namespace('')
>>> print(Element(xhtml.html, lang='en'))
<html xmlns="" lang="en"/>
Instance Methods
__init__(self, tag_, **attrib)
Create a new fragment.
__call__(self, *args, **kwargs)
Append any positional arguments as child nodes, and keyword arguments as attributes.
Return a markup event stream for the fragment.

Inherited from Fragment: __add__, __html__, __iter__, __str__, __unicode__, append

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__


Inherited from Fragment: children

Inherited from object: __class__

Method Details

__init__(self, tag_, **attrib)

Create a new fragment.
Overrides: object.__init__
(inherited documentation)

__call__(self, *args, **kwargs)
(Call operator)

Append any positional arguments as child nodes, and keyword arguments as attributes.
Returns: Element
the element itself so that calls can be chained
Overrides: Fragment.__call__

See Also: Fragment.append

(Representation operator)

Overrides: object.__repr__
(inherited documentation)


Return a markup event stream for the fragment.
Returns: Stream
Overrides: Fragment.generate