Go to the latest version of this document.
search in docstrings

Search bar offers the following options:

- Term presence. The below example searches for documents that must contain “foo”, might contain “bar” and must not contain “baz”: `+foo bar -baz`
- Wildcards. The below example searches for documents with words beginning with “foo”: `foo*`
- Search in specific fields. The following search matches all objects in "twisted.mail" that matches “search”: `+qname:twisted.mail.* +search`

  Possible fields: 'name', 'qname' (fully qualified name), 'docstring', and 'kind'. Last two fields are only applicable if "search in docstrings" is enabled.

- Fuzzy matches. The following search matches all documents that have a word within 1 edit distance of “foo”: `foo~1`

Results provided by Lunr.js

# ``twisted`.`words`.`xish`.`domish`.`Element``

class documentation

`class Element: (source)`

Known subclasses: `twisted.words.protocols.jabber.client.IQ`, `twisted.words.protocols.jabber.xmlstream.IQ`

Implements interfaces: `twisted.words.xish.domish.IElement`

View In Hierarchy

Represents an XML element node.

An Element contains a series of attributes (name/value pairs), content (character data), and other child Element objects. When building a document with markup (such as HTML or XML), use this object as the starting point.

Element objects fully support XML Namespaces. The fully qualified name of the XML Element it represents is stored in the `uri` and `name` attributes, where `uri` holds the namespace URI. There is also a default namespace, for child elements. This is stored in the `defaultUri` attribute. Note that `''` means the empty namespace.

Serialization of Elements through `toXml()` will use these attributes for generating proper serialized XML. When both `uri` and `defaultUri` are not None in the Element and all of its descendents, serialization proceeds as expected:

    >>> from twisted.words.xish import domish
    >>> root = domish.Element(('myns', 'root'))
    >>> root.addElement('child', content='test')
    <twisted.words.xish.domish.Element object at 0x83002ac>
    >>> root.toXml()
    u"<root xmlns='myns'><child>test</child></root>"

For partial serialization, needed for streaming XML, a special value for namespace URIs can be used: `None`.

Using `None` as the value for `uri` means: this element is in whatever namespace inherited by the closest logical ancestor when the complete XML document has been serialized. The serialized start tag will have a non-prefixed name, and no xmlns declaration will be generated.

Similarly, `None` for `defaultUri` means: the default namespace for my child elements is inherited from the logical ancestors of this element, when the complete XML document has been serialized.

To illustrate, an example from a Jabber stream. Assume the start tag of the root element of the stream has already been serialized, along with several complete child elements, and sent off, looking like this:

      <stream:stream xmlns:stream='http://etherx.jabber.org/streams'
                     xmlns='jabber:client' to='example.com'>
        ...

Now suppose we want to send a complete element represented by an object `message` created like:

    >>> message = domish.Element((None, 'message'))
    >>> message['to'] = 'user@example.com'
    >>> message.addElement('body', content='Hi!')
    <twisted.words.xish.domish.Element object at 0x8276e8c>
    >>> message.toXml()
    u"<message to='user@example.com'><body>Hi!</body></message>"

As, you can see, this XML snippet has no xmlns declaration. When sent off, it inherits the `jabber:client` namespace from the root element. Note that this renders the same as using `''` instead of `None`:

    >>> presence = domish.Element(('', 'presence'))
    >>> presence.toXml()
    u"<presence/>"

However, if this object has a parent defined, the difference becomes clear:

    >>> child = message.addElement(('http://example.com/', 'envelope'))
    >>> child.addChild(presence)
    <twisted.words.xish.domish.Element object at 0x8276fac>
    >>> message.toXml()
    u"<message to='user@example.com'><body>Hi!</body><envelope xmlns='http://example.com/'><presence xmlns=''/></envelope></message>"

As, you can see, the <presence/> element is now in the empty namespace, not in the default namespace of the parent or the streams'.

Method | `__bytes__` | Retrieve the first character data node as UTF-8 bytes.
Method | `__delitem__` | Undocumented
Method | `__getattr__` | Undocumented
Method | `__getitem__` | Undocumented
Method | `__init__` | No summary
Method | `__setitem__` | Undocumented
Method | `__unicode__` | Retrieve the first CData (content) node
Method | `addChild` | Add a child to this Element.
Method | `addContent` | Add some text data to this Element.
Method | `addElement` | Create an element and add as child.
Method | `addRawXml` | Add a pre-serialized chunk o' XML as a child of this Element.
Method | `addUniqueId` | Add a unique (across a given Python session) id attribute to this Element.
Method | `compareAttribute` | Safely compare the value of an attribute against a provided value.
Method | `elements` | Iterate across all children of this Element that are Elements.
Method | `firstChildElement` | Undocumented
Method | `getAttribute` | Retrieve the value of attribname, if it exists
Method | `hasAttribute` | Determine if the specified attribute exists
Method | `swapAttributeValues` | Swap the values of two attribute.
Method | `toXml` | Serialize this Element and all children to a string.
Instance Variable | `attributes` | Dictionary of attributes associated with this Element.
Instance Variable | `children` | List of child Elements and content
Instance Variable | `defaultUri` | URI this Element exists within
Instance Variable | `localPrefixes` | Dictionary of namespace declarations on this element. The key is the prefix to bind the namespace uri to.
Instance Variable | `name` | Name of this Element
Instance Variable | `parent` | Reference to the parent Element, if any.
Instance Variable | `uri` | URI of this Element's name
Method | `_dqa` | Dequalify an attribute key as needed
Class Variable | `_idCounter` | Undocumented

def __bytes__(self): (source)

Retrieve the first character data node as UTF-8 bytes.

def __delitem__(self, key): (source)

Undocumented

def __getattr__(self, key): (source)

Undocumented

def __getitem__(self, key): (source)

Undocumented

def __init__(self, qname, defaultUri=None, attribs=None, localPrefixes=None): (source)
overridden in `twisted.words.protocols.jabber.client.IQ`, `twisted.words.protocols.jabber.xmlstream.IQ`

Parameters
qname | Tuple of (uri, name)
defaultUri | The default URI of the element; defaults to the URI specified in `qname`
attribs | Dictionary of attributes
localPrefixes | Dictionary of namespace declarations on this element. The key is the prefix to bind the namespace uri to.

def __setitem__(self, key, value): (source)

Undocumented

def __unicode__(self): (source)

Retrieve the first CData (content) node

def addChild(self, node): (source)
from `twisted.words.xish.domish.IElement`

Add a child to this Element.

def addContent(self, text): (source)
from `twisted.words.xish.domish.IElement`

Add some text data to this Element.

Parameters
text:`str` | Undocumented
Returns
`str` | Undocumented

def addElement(self, name, defaultUri=None, content=None): (source)
from `twisted.words.xish.domish.IElement`

Create an element and add as child.

The new element is added to this element as a child, and will have this element as its parent.

Parameters
name:`str` or `tuple` of (`str`, `str`) | element name. This can be either a `str` object that contains the local name, or a tuple of (uri, local_name) for a fully qualified name. In the former case, the namespace URI is inherited from this element.
defaultUri:`str` | default namespace URI for child elements. If `None`, this is inherited from this element.
content:`str` | text contained by the new element.
Returns
object providing `IElement` | the created element

def addRawXml(self, rawxmlstring): (source)

Add a pre-serialized chunk o' XML as a child of this Element.

def addUniqueId(self): (source)

Add a unique (across a given Python session) id attribute to this Element.

def compareAttribute(self, attrib, value): (source)

Safely compare the value of an attribute against a provided value.

`None`-safe.

def elements(self, uri=None, name=None): (source)

Iterate across all children of this Element that are Elements.

Returns a generator over the child elements. If both the `uri` and `name` parameters are set, the returned generator will only yield on elements matching the qualified name.

Parameters
uri:`str` | Optional element URI.
name:`str` | Optional element name.
Returns
Iterator that yields objects implementing `IElement`.

def firstChildElement(self): (source)

Undocumented

def getAttribute(self, attribname, default=None): (source)

Retrieve the value of attribname, if it exists

def hasAttribute(self, attrib): (source)

Determine if the specified attribute exists

def swapAttributeValues(self, left, right): (source)

Swap the values of two attribute.

def toXml(self, prefixes=None, closeElement=1, defaultUri='', prefixesInScope=None): (source)
from `twisted.words.xish.domish.IElement`

Serialize this Element and all children to a string.

attributes: `dict` = (source)
from `twisted.words.xish.domish.IElement`

Dictionary of attributes associated with this Element.

children: `list` = (source)
from `twisted.words.xish.domish.IElement`

List of child Elements and content

defaultUri: `str` or None = (source)
from `twisted.words.xish.domish.IElement`

URI this Element exists within

localPrefixes: `dict` = (source)
from `twisted.words.xish.domish.IElement`

Dictionary of namespace declarations on this element. The key is the prefix to bind the namespace uri to.

name: `str` = (source)
from `twisted.words.xish.domish.IElement`

Name of this Element

parent: `Element` = (source)
from `twisted.words.xish.domish.IElement`

Reference to the parent Element, if any.

uri: `str` or None = (source)
from `twisted.words.xish.domish.IElement`

URI of this Element's name

def _dqa(self, attr): (source)

Dequalify an attribute key as needed

_idCounter: `int` = (source)

Undocumented