Package genshi :: Module builder :: Class Element

Class Element

object --+    
         |    
  Fragment --+
             |
            Element

Simple XML output generator based on the builder pattern.

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

>>> print(Element('strong'))
<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))
<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.

>>> print(Element('ul')(Element('li'), Element('li')))
<ul><li/><li/></ul>
>>> print(Element('a')('Label'))
<a>Label</a>
>>> 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))
<em>42</em>
>>> 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('http://www.w3.org/1999/xhtml')
>>> print(Element(xhtml.html, lang='en'))
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"/>
Instance Methods
 
__init__(self, tag_, **attrib)
Create a new fragment.
Element
__call__(self, *args, **kwargs)
Append any positional arguments as child nodes, and keyword arguments as attributes.
 
__repr__(self)
repr(x)
Stream
generate(self)
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__

Properties
  attrib
  tag

Inherited from Fragment: children

Inherited from object: __class__

Method Details

__init__(self, tag_, **attrib)
(Constructor)

 
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

__repr__(self)
(Representation operator)

 
repr(x)
Overrides: object.__repr__
(inherited documentation)

generate(self)

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