xelement module

Provides the XElement, which extends the ElementTree.Element functionalities


Module classes summary

XElement

Extended ElementTree.Element with additional useful methods.


Module classes

class XElement(tag, attrib={}, text=None, **extra)

Bases: Element

Extended ElementTree.Element with additional useful methods.


__init__(tag, attrib={}, text=None, **extra)
append(subelement, /)
attrib

Dictionary of the element’s attributes.


clear()
extend(elements, /)
find(path, namespaces=None)
findall(paths, namespaces=None)

Extesion of findall to return a list of nodes matching one or multiple tag or Xpath strings.

Parameters:

paths (str or list of str) – If paths is a string, search for the nodes with the tag or XPath it specifies. If paths is a list of strings, returns nodes matchin all the specified tags or Xpaths.

Returns: list: list of nodes matching the specified tag(s) or Xpath(s). If multiple strings

are provided, the same nodes may appear multiple times, and nodes are not listed in their order of appearance in the tree.


findallbetween(tag, start_at=None, start_after=None, stop_before=None, stop_at=None, recurse=True)
findindex(elem_or_path)

Like find(), looks recursively for an element, but returns both the parent and index of the element. Also can search for an object in addition to a tag or path.

Parameters:

elem_or_path (str or Element) – element to search.


findsibling(elem_or_path, offset=1)
findsubtext(path, default=None, namespaces=None)

Returns the combined text of the element specified by path and all its children, crossing all hierarchy levels


findtext(path, default=None, namespaces=None)
findwithsubtext(path, text, caseless=False)
findwithtext(text, path='.//*', caseless=False)
get(key, default=None)
group(element_list, tag)

Group the elements in element_list into a new XElement with tag tag.

Parameters:
  • element_list (list) – List of XElements to group. If element_list is empty of is None, nothing is done.

  • tag (str) – Tag of the new parent Xelement that contains the specified elements


index(children)

Returns the the index of a children within this element only (does not search into the children elements; see findindex() for this.)

Parameters:

children (Element) – Element object for which we seek the index

Returns:

index of object.

Return type:

int

Exceptions:

Raises a ValueError if the children cannot be found


insert(index, subelement, /)
items()
iter(tag=None)
iterbetween(start_at=None, start_after=None, stop_before=None, stop_at=None, recurse=True)

Same as iter(), excepts this method yields only the elements between the specified starting and ending nodes.

Parameters:
  • start_at (str) – Xpath describing the first element to be included

  • start_after (str) – Xpath describing the element after which the elements will be included

  • stop_before (str) – Xpath indicating the element before which the elements will be included.

  • stop_at (str) – Xpath indicating the last element to be included.

Notes

  • Both start options should not be provided. If so, start_after will takes precedence over start_at.

  • Both end options should not be provided. If so, stop_before will takes precedence over stop_at.

  • The end nodes should not appear before the start nodes


iterfind(path, namespaces=None)
itertext()
keys()
makeelement(tag, attrib, /)
move(element, target_element, index=None)

Move element(s) element into target_element at specified position.

Parameters:
  • element (list or XElement) – Element(s) to move. If element is a list or tuple, move all the specified elements.

  • target_element (XElement) – Destination element

  • index (int or None) – If index = None or index = -1, appends data to the target element. If index >= 0, insert the elements starting at the specified index on the target element.


pp(level=0, collapsed_expr=['comment', 'target', 'name', 'expression', 'simple_expression', 'simple_name', 'association_element', '_'], max_depth=0, width=80)

Pretty printer for the XElement tree.


remove(subelement, /)
set(key, value, /)
property subtext

Return the text of this element and all its subelements.


subtextbetween(start_at=None, start_after=None, end_before=None, end_after=None)

Like subtext(), but only returns the contatenated text between the specified starting and ending elements, crossing hierarchy.


tag

The element’s name.


tail

Text after this element’s end tag, but before the next sibling element’s start tag. This is either a string or the value None. Note that if there was no text, this attribute may be either None or an empty string, depending on the parser.


text

Text before first subelement. This is either a string or the value None. Note that if there is no text, this attribute may be either None or the empty string, depending on the parser.