Package genshi :: Module builder :: Class ElementFactory

Class ElementFactory

object --+
         |
        ElementFactory

Factory for Element objects.

A new element is created simply by accessing a correspondingly named attribute of the factory object:

>>> factory = ElementFactory()
>>> print(factory.foo)
<foo/>
>>> print(factory.foo(id=2))
<foo id="2"/>

Markup fragments (lists of nodes without a parent element) can be created by calling the factory:

>>> print(factory('Hello, ', factory.em('world'), '!'))
Hello, <em>world</em>!

A factory can also be bound to a specific namespace:

>>> factory = ElementFactory('http://www.w3.org/1999/xhtml')
>>> print(factory.html(lang="en"))
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"/>

The namespace for a specific element can be altered on an existing factory by specifying the new namespace using item access:

>>> factory = ElementFactory()
>>> print(factory.html(factory['http://www.w3.org/2000/svg'].g(id=3)))
<html><g xmlns="http://www.w3.org/2000/svg" id="3"/></html>

Usually, the ElementFactory class is not be used directly. Rather, the tag instance should be used to create elements.

Instance Methods
 
__init__(self, namespace=None)
Create the factory, optionally bound to the given namespace.
Fragment
__call__(self, *args)
Create a fragment that has the given positional arguments as child nodes.
ElementFactory
__getitem__(self, namespace)
Return a new factory that is bound to the specified namespace.
Element
__getattr__(self, name)
Create an Element with the given name.

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

Properties

Inherited from object: __class__

Method Details

__init__(self, namespace=None)
(Constructor)

 
Create the factory, optionally bound to the given namespace.
Parameters:
  • namespace - the namespace URI for any created elements, or None for no namespace
Overrides: object.__init__

__call__(self, *args)
(Call operator)

 
Create a fragment that has the given positional arguments as child nodes.
Returns: Fragment
the created Fragment

__getitem__(self, namespace)
(Indexing operator)

 
Return a new factory that is bound to the specified namespace.
Parameters:
  • namespace - the namespace URI or Namespace object
Returns: ElementFactory
an ElementFactory that produces elements bound to the given namespace

__getattr__(self, name)
(Qualification operator)

 
Create an Element with the given name.
Parameters:
  • name - the tag name of the element to create
Returns: Element
an Element with the specified name