sarkit.xmlhelp.ElementWrapper

class ElementWrapper(elem, xsdhelper, wrapped_parent=None, typename=None, elementpath=None, roottag=None)

Wrapper for lxml.etree.Element that provides dictionary-ish interface

Getting/setting of schema-valid subelements does not inherently raise an exception if they don’t exist. Setter will create non-existent ancestors as necessary. Repeatable elements are treated as tuples. Transcoded values are copies, not references. Some effort has been made to make them immutable. If you manage to change them, the changes are not reflected in the underlying XML element. Attributes are accessed using BadgerFish notation (e.g. @attr). Keys are local names. Namespaces are handled automatically.

Parameters:
elemlxml.etree.Element or None

Element to wrap or None for a placeholder element. If elem is None, elementpath must be set.

xsdhelperXsdHelper

XML helper for the root namespace of the document that contains elem

wrapped_parentElementWrapper or None

Wrapper for the parent of elem or None. Only used when trying to set an item of a wrapped placeholder.

typenamestr or None

XSD typename of the element being wrapped. If None, the type information is retrieved from the XML Helper.

elementpathstr or None

ElementPath expression that identifies the location of elem in its tree. If None, the path is retrieved from the element. Required if elem is None.

roottagstr or None

Tag of the root element of elem’s tree. If None, the tag is retrieved from the element. Required if elem is None.

Methods

add(localname[, val])

Add a new subelement and optionally set its value.

find(localname, **kwargs)

Returns first child with given localname with children described by kwargs or None if none exist.

findall(localname, **kwargs)

Returns a list of all children with given localname with children described by kwargs.

from_dict(val)

Populate the ElementWrapper with the contents of a dictionary.

get(localname[, default])

Return value from an ElementWrapper.

setdefault(localname[, default])

Return value from an ElementWrapper if present.

to_dict()

Recursively convert the ElementWrapper to a dictionary.

Notes

The wrapped element must be located in a tree with a root element expected by xsdhelper.