Nodes & Filters¶
Comment¶
- class delb.CommentNode(etree_element: _Element)[source]¶
The instances of this class represent comment nodes of a tree.
To instantiate new nodes use
new_comment_node().Node properties:
The depth (or level) of the node in its tree.
The node's index within the parent's collection of child nodes or
Nonewhen the node has no parent.The prefix to namespace mapping of the node.
Node content properties:
The comment's text.
The concatenated contents of all text node descendants in document order.
Related document and nodes properties:
The
Documentinstance that the node is associated with orNone.The node's first child.
The node's last child node.
The node's last descendant.
The node's parent or
None.Methods to query the tree:
Queries the tree with an XPath expression with this node as initial context node.
Methods to fetch a relative node:
Retrieves the next filter matching node on the following axis.
Retrieves the next filter matching node on the following-sibling axis.
Retrieves the next filter matching node on the preceding axis.
Retrieves the next filter matching node on the preceding-sibling axis.
Methods to iterate over related node:
Iterator over the filter matching nodes on the ancestor axis.
A generator iterator that yields nothing.
A generator iterator that yields nothing.
Iterator over the filter matching nodes on the following axis.
Iterator over the filter matching nodes on the following-sibling axis.
Iterator over the filter matching nodes on the preceding axis.
Iterator over the filter matching nodes on the preceding-sibling axis.
Methods to add nodes to a tree:
Adds one or more nodes to the right of the node this method is called on.
Adds one or more nodes to the left of the node this method is called on.
Methods to remove a node:
Removes the node from its tree.
Removes the node and places the given one in its tree location.
Methods:
Creates a new node of the same type with duplicated contents.
Creates a new
TagNodeinstance in the node's context.Returns a string that contains the serialization of the node.
- add_following_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the right of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- add_preceding_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the left of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- clone(deep: bool = False, quick_and_unsafe: bool | None = None) _ElementWrappingNode¶
Creates a new node of the same type with duplicated contents.
- Parameters:
deep – Clones the whole subtree if
True.quick_and_unsafe – Deprecated.
- Returns:
A copy of the node.
- detach(retain_child_nodes: bool = False) _ElementWrappingNode¶
Removes the node from its tree.
- Parameters:
retain_child_nodes – Keeps the node’s descendants in the originating tree if
True.- Returns:
The removed node.
- fetch_following(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following axis.
- fetch_following_sibling(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following-sibling axis.
- fetch_preceding(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the preceding axis.
- fetch_preceding_sibling(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the preceding-sibling axis.
- first_child = None¶
The node’s first child.
- property index: int | None¶
The node’s index within the parent’s collection of child nodes or
Nonewhen the node has no parent.
- iterate_ancestors(*filter: delb.typing.Filter) Iterator[TagNode]¶
Iterator over the filter matching nodes on the ancestor axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the ancestor nodes from bottom to top.
- iterate_children(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
A generator iterator that yields nothing.
- iterate_descendants(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
A generator iterator that yields nothing.
- iterate_following(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the following nodes in document order.
- iterate_following_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s right.
- iterate_preceding(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the previous nodes in document order.
- iterate_preceding_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s left.
- last_child = None¶
The node’s last child node.
- last_descendant = None¶
The node’s last descendant.
- new_tag_node(local_name: str, attributes: dict[AttributeAccessor, str] | None = None, namespace: str | None = None, children: Sequence[str | NodeBase | _TagDefinition] = ()) TagNode¶
Creates a new
TagNodeinstance in the node’s context.- Parameters:
local_name – The tag name.
attributes – Optional attributes that are assigned to the new node.
namespace – An optional tag namespace. If none is provided, the context node’s namespace is inherited.
children – An optional sequence of objects that will be appended as child nodes. This can be existing nodes, strings that will be inserted as text nodes and in-place definitions of
TagNodeinstances fromtag(). The latter will be assigned to the same namespace.
- Returns:
The newly created tag node.
- replace_with(node: delb.typing.NodeSource, clone: bool = False) delb.NodeBase¶
Removes the node and places the given one in its tree location.
The node can be a concrete instance of any node type or a rather abstract description in the form of a string or an object returned from the
tag()function that is used to derive aTextNoderespectivelyTagNodeinstance from.- Parameters:
node – The replacing node.
clone – A concrete, replacing node is cloned if
True.
- Returns:
The removed node.
- serialize(*, format_options: FormatOptions | None = None, namespaces: delb.typing.NamespaceDeclarations | None = None, newline: str | None = None)¶
Returns a string that contains the serialization of the node. See Serialization for details.
- Parameters:
format_options – An instance of
FormatOptionscan be provided to configure formatting.namespaces – A mapping of prefixes to namespaces. These are overriding possible declarations from a parsed serialisat that the document instance stems from. Prefixes for undeclared namespaces are enumerated with the prefix
ns.newline – See
io.TextIOWrapperfor a detailed explanation of the parameter with the same name.
- xpath(expression: str, namespaces: delb.typing.NamespaceDeclarations | None = None) QueryResults¶
Queries the tree with an XPath expression with this node as initial context node.
- Parameters:
expression – A supported XPath 1.0 expression that contains one or more location paths.
namespaces – A mapping of prefixes that are used in the expression to namespaces. The declarations that were used in a document’s source serialisat serve as fallback.
- Returns:
All nodes that match the evaluation of the provided XPath expression.
See Queries with XPath & CSS for details on the extent of the XPath implementation.
Processing instruction¶
- class delb.ProcessingInstructionNode(etree_element: _Element)[source]¶
The instances of this class represent processing instruction nodes of a tree.
To instantiate new nodes use
new_processing_instruction_node().Node properties:
The depth (or level) of the node in its tree.
The node's index within the parent's collection of child nodes or
Nonewhen the node has no parent.The prefix to namespace mapping of the node.
Node content properties:
The processing instruction's text.
The concatenated contents of all text node descendants in document order.
The processing instruction's target.
Related document and nodes properties:
The
Documentinstance that the node is associated with orNone.The node's first child.
The node's last child node.
The node's last descendant.
The node's parent or
None.Methods to query the tree:
Queries the tree with an XPath expression with this node as initial context node.
Methods to fetch a relative node:
Retrieves the next filter matching node on the following axis.
Retrieves the next filter matching node on the following-sibling axis.
Retrieves the next filter matching node on the preceding axis.
Retrieves the next filter matching node on the preceding-sibling axis.
Methods to iterate over related node:
Iterator over the filter matching nodes on the ancestor axis.
A generator iterator that yields nothing.
A generator iterator that yields nothing.
Iterator over the filter matching nodes on the following axis.
Iterator over the filter matching nodes on the following-sibling axis.
Iterator over the filter matching nodes on the preceding axis.
Iterator over the filter matching nodes on the preceding-sibling axis.
Methods to add nodes to a tree:
Adds one or more nodes to the right of the node this method is called on.
Adds one or more nodes to the left of the node this method is called on.
Methods to remove a node:
Removes the node from its tree.
Removes the node and places the given one in its tree location.
Methods:
Creates a new node of the same type with duplicated contents.
Creates a new
TagNodeinstance in the node's context.Returns a string that contains the serialization of the node.
- add_following_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the right of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- add_preceding_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the left of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- clone(deep: bool = False, quick_and_unsafe: bool | None = None) _ElementWrappingNode¶
Creates a new node of the same type with duplicated contents.
- Parameters:
deep – Clones the whole subtree if
True.quick_and_unsafe – Deprecated.
- Returns:
A copy of the node.
- detach(retain_child_nodes: bool = False) _ElementWrappingNode¶
Removes the node from its tree.
- Parameters:
retain_child_nodes – Keeps the node’s descendants in the originating tree if
True.- Returns:
The removed node.
- fetch_following(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following axis.
- fetch_following_sibling(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following-sibling axis.
- fetch_preceding(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the preceding axis.
- fetch_preceding_sibling(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the preceding-sibling axis.
- first_child = None¶
The node’s first child.
- property index: int | None¶
The node’s index within the parent’s collection of child nodes or
Nonewhen the node has no parent.
- iterate_ancestors(*filter: delb.typing.Filter) Iterator[TagNode]¶
Iterator over the filter matching nodes on the ancestor axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the ancestor nodes from bottom to top.
- iterate_children(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
A generator iterator that yields nothing.
- iterate_descendants(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
A generator iterator that yields nothing.
- iterate_following(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the following nodes in document order.
- iterate_following_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s right.
- iterate_preceding(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the previous nodes in document order.
- iterate_preceding_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s left.
- last_child = None¶
The node’s last child node.
- last_descendant = None¶
The node’s last descendant.
- new_tag_node(local_name: str, attributes: dict[AttributeAccessor, str] | None = None, namespace: str | None = None, children: Sequence[str | NodeBase | _TagDefinition] = ()) TagNode¶
Creates a new
TagNodeinstance in the node’s context.- Parameters:
local_name – The tag name.
attributes – Optional attributes that are assigned to the new node.
namespace – An optional tag namespace. If none is provided, the context node’s namespace is inherited.
children – An optional sequence of objects that will be appended as child nodes. This can be existing nodes, strings that will be inserted as text nodes and in-place definitions of
TagNodeinstances fromtag(). The latter will be assigned to the same namespace.
- Returns:
The newly created tag node.
- replace_with(node: delb.typing.NodeSource, clone: bool = False) delb.NodeBase¶
Removes the node and places the given one in its tree location.
The node can be a concrete instance of any node type or a rather abstract description in the form of a string or an object returned from the
tag()function that is used to derive aTextNoderespectivelyTagNodeinstance from.- Parameters:
node – The replacing node.
clone – A concrete, replacing node is cloned if
True.
- Returns:
The removed node.
- serialize(*, format_options: FormatOptions | None = None, namespaces: delb.typing.NamespaceDeclarations | None = None, newline: str | None = None)¶
Returns a string that contains the serialization of the node. See Serialization for details.
- Parameters:
format_options – An instance of
FormatOptionscan be provided to configure formatting.namespaces – A mapping of prefixes to namespaces. These are overriding possible declarations from a parsed serialisat that the document instance stems from. Prefixes for undeclared namespaces are enumerated with the prefix
ns.newline – See
io.TextIOWrapperfor a detailed explanation of the parameter with the same name.
- xpath(expression: str, namespaces: delb.typing.NamespaceDeclarations | None = None) QueryResults¶
Queries the tree with an XPath expression with this node as initial context node.
- Parameters:
expression – A supported XPath 1.0 expression that contains one or more location paths.
namespaces – A mapping of prefixes that are used in the expression to namespaces. The declarations that were used in a document’s source serialisat serve as fallback.
- Returns:
All nodes that match the evaluation of the provided XPath expression.
See Queries with XPath & CSS for details on the extent of the XPath implementation.
Tag¶
- class delb.TagNode(etree_element: _Element)[source]¶
The instances of this class represent tag node s of a tree, the equivalent of DOM’s elements.
To instantiate new nodes use
Document.new_tag_node,TagNode.new_tag_node,TextNode.new_tag_nodeornew_tag_node().Some syntactic sugar is baked in:
Attributes and nodes can be tested for membership in a node.
>>> root = Document('<root ham="spam"><child/></root>').root >>> "ham" in root True >>> root.first_child in root True
Nodes can be copied. Note that this relies on
TagNode.clone().>>> from copy import copy, deepcopy >>> root = Document("<root>Content</root>").root >>> print(copy(root)) <root/> >>> print(deepcopy(root)) <root>Content</root>
Attribute values and child nodes can be obtained, set and deleted with the subscript notation.
>>> root = Document('<root x="y"><child_1/>child_2<child_3/></root>').root >>> print(root["x"]) y >>> print(root[0]) <child_1/> >>> print(root[-1]) <child_3/> >>> print([str(x) for x in root[1::-1]]) ['child_2', '<child_1/>']
How much child nodes has this node anyway?
>>> root = Document("<root><child_1/><child_2/></root>").root >>> len(root) 2 >>> len(root[0]) 0
As seen in the examples above, a tag nodes string representation yields a serialized XML representation of a sub-/tree. See Serialization for details.
Node properties:
The depth (or level) of the node in its tree.
The node's index within the parent's collection of child nodes or
Nonewhen the node has no parent.The node's name.
An unambiguous XPath location path that points to this node from its tree root.
The node's namespace.
The prefix to namespace mapping of the node.
The prefix that the node's namespace is currently mapped to.
The node's qualified name in Clark notation.
Node content properties:
A mapping that can be used to access the node's attributes.
The concatenated contents of all text node descendants in document order.
This is a shortcut to retrieve and set the
idattribute in the XML namespace.Related document and nodes properties:
The
Documentinstance that the node is associated with orNone.The node's first child node.
The node's last child node.
The node's last descendant.
The node's parent or
None.Methods to query the tree:
Queries the tree with a CSS selector expression with this node as initial context node.
Fetches a single node that is locatable by the provided XPath expression.
Queries the tree with an XPath expression with this node as initial context node.
Methods to fetch a relative node:
Retrieves the next filter matching node on the following axis.
Retrieves the next filter matching node on the following-sibling axis.
Retrieves the next filter matching node on the preceding axis.
Retrieves the next filter matching node on the preceding-sibling axis.
Methods to iterate over related node:
Iterator over the filter matching nodes on the ancestor axis.
Iterator over the filter matching nodes on the child axis.
Iterator over the filter matching nodes on the ancestor axis.
Iterator over the filter matching nodes on the following axis.
Iterator over the filter matching nodes on the following-sibling axis.
Iterator over the filter matching nodes on the preceding axis.
Iterator over the filter matching nodes on the preceding-sibling axis.
Methods to add nodes to a tree:
Adds one or more nodes to the right of the node this method is called on.
Adds one or more nodes to the left of the node this method is called on.
Adds one or more nodes as child nodes after any existing to the child nodes of the node this method is called on.
Inserts one or more child nodes.
Adds one or more nodes as child nodes before any existing to the child nodes of the node this method is called on.
Methods to remove a node:
Removes the node from its tree.
Removes the node and places the given one in its tree location.
Methods:
Creates a new node of the same type with duplicated contents.
Merges all consecutive text nodes in the subtree into one.
Creates a new
TagNodeinstance in the node's context.Parses the given string or bytes sequence into a new tree.
Returns a string that contains the serialization of the node.
- add_following_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the right of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- add_preceding_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the left of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- append_children(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...][source]¶
Adds one or more nodes as child nodes after any existing to the child nodes of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were appended.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- property attributes: TagAttributes¶
A mapping that can be used to access the node’s attributes.
>>> node = new_tag_node("node", attributes={"foo": "0", "bar": "0"}) >>> node.attributes {'foo': '0', 'bar': '0'} >>> node.attributes.pop("bar") <Attribute(bar="0") [0x...]> >>> node.attributes["foo"] = "1" >>> node.attributes["peng"] = "1" >>> print(node) <node foo="1" peng="1"/> >>> node.attributes.update({"foo": "2", "zong": "2"}) >>> print(node) <node foo="2" peng="1" zong="2"/>
Namespaced names are accessed with two-value tuples or a string. The two-value holds the namespace and the local name in that order. A string can either be a fully qualified name in Clark notation or a local name that belongs to the containing node’s namespace.
>>> DefaultStringOptions.namespaces = {None: "http://namespace"} >>> node = new_tag_node( ... "node", ... namespace="http://namespace", ... ) >>> node.attributes.update({("http://namespace", "foo"): "0"}) >>> print(node) <node xmlns="http://namespace" foo="0"/> >>> attribute = node.attributes[("http://namespace", "foo")] >>> node.attributes["foo"] is attribute True >>> node.attributes["{http://namespace}foo"] is attribute True
Attributes behave like strings, but also expose namespace, local name and value for manipulation.
>>> node = new_tag_node("node") >>> node.attributes["foo"] = "0" >>> node.attributes["foo"].local_name = "bar" >>> node.attributes["bar"].namespace = "http://namespace" >>> node.attributes[("http://namespace", "bar")].value = "X" >>> print(node) <node xmlns:ns0="http://namespace" ns0:bar="X"/> >>> "ref-" + node.attributes[("http://namespace", "bar")].lower() 'ref-x'
- clone(deep: bool = False, quick_and_unsafe: bool | None = None) TagNode[source]¶
Creates a new node of the same type with duplicated contents.
- Parameters:
deep – Clones the whole subtree if
True.quick_and_unsafe – Deprecated.
- Returns:
A copy of the node.
- css_select(expression: str, namespaces: delb.typing.NamespaceDeclarations | None = None) QueryResults[source]¶
Queries the tree with a CSS selector expression with this node as initial context node.
- Parameters:
expression – A CSS selector expression.
namespaces – A mapping of prefixes that are used in the expression to namespaces. If omitted, the node’s definition is used.
- Returns:
All nodes that match the evaluation of the provided CSS selector expression.
See Queries with XPath & CSS regarding the extent of the supported grammar.
Namespace prefixes are delimited with a
|before a name test, for examplediv svg|metadataselects all descendants ofdivnamed nodes that belong to the default namespace or have no namespace and whose name ismetadataand have a namespace that is mapped to thesvgprefix.
- detach(retain_child_nodes: bool = False) _ElementWrappingNode[source]¶
Removes the node from its tree.
- Parameters:
retain_child_nodes – Keeps the node’s descendants in the originating tree if
True.- Returns:
The removed node.
- fetch_following(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following axis.
- fetch_following_sibling(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following-sibling axis.
- fetch_or_create_by_xpath(expression: str, namespaces: delb.typing.NamespaceDeclarations | None = None) TagNode[source]¶
Fetches a single node that is locatable by the provided XPath expression. If the node doesn’t exist, the non-existing branch will be created.
- Parameters:
expression – An XPath expression that can unambiguously locate a descending node in a tree that has any state.
namespaces – An optional mapping of prefixes to namespaces. The declarations that were used in a document’s source serialisat serve as fallback.
- Returns:
The existing or freshly created node descibed with
expression.
These rules are imperative in your endeavour:
All location steps must use the child axis.
Each step needs to provide a name test.
Attribute comparisons against literals are the only allowed predicates.
Multiple attribute comparisons must be joined with the and operator and / or contained in more than one predicate expression.
The logical validity of multiple attribute comparisons isn’t checked. E.g. one could provide
foo[@p="her"][@p="him"], but expect an undefined behaviour.
>>> root = Document("<root/>").root >>> grandchild = root.fetch_or_create_by_xpath( ... "child[@a='b']/grandchild" ... ) >>> grandchild is root.fetch_or_create_by_xpath( ... "child[@a='b']/grandchild" ... ) True >>> str(root) '<root><child a="b"><grandchild/></child></root>'
- fetch_preceding(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the preceding axis.
- fetch_preceding_sibling(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the preceding-sibling axis.
- property first_child: delb.NodeBase | None¶
The node’s first child node.
- property id: str | None¶
This is a shortcut to retrieve and set the
idattribute in the XML namespace. The client code is responsible to pass properly formed id names.
- property index: int | None¶
The node’s index within the parent’s collection of child nodes or
Nonewhen the node has no parent.
- insert_children(index: int, *node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...][source]¶
Inserts one or more child nodes.
- Parameters:
index – The index at which the first of the given nodes will be inserted, the remaining nodes are added afterwards in the given order.
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were inserted.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- iterate_ancestors(*filter: delb.typing.Filter) Iterator[TagNode]¶
Iterator over the filter matching nodes on the ancestor axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the ancestor nodes from bottom to top.
- iterate_children(*filter: delb.typing.Filter) Iterator[delb.NodeBase][source]¶
Iterator over the filter matching nodes on the child axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the child nodes of the node.
- iterate_descendants(*filter: delb.typing.Filter) Iterator[delb.NodeBase][source]¶
Iterator over the filter matching nodes on the ancestor axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the descending nodes of the node.
- iterate_following(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the following nodes in document order.
- iterate_following_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s right.
- iterate_preceding(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the previous nodes in document order.
- iterate_preceding_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s left.
- property last_child: delb.NodeBase | None¶
The node’s last child node.
- property last_descendant: delb.NodeBase | None¶
The node’s last descendant.
- property location_path: str¶
An unambiguous XPath location path that points to this node from its tree root.
- merge_text_nodes()[source]¶
Merges all consecutive text nodes in the subtree into one. Text nodes without content are dropped.
- new_tag_node(local_name: str, attributes: dict[AttributeAccessor, str] | None = None, namespace: str | None = None, children: Sequence[str | NodeBase | _TagDefinition] = ()) TagNode[source]¶
Creates a new
TagNodeinstance in the node’s context.- Parameters:
local_name – The tag name.
attributes – Optional attributes that are assigned to the new node.
namespace – An optional tag namespace. If none is provided, the context node’s namespace is inherited.
children – An optional sequence of objects that will be appended as child nodes. This can be existing nodes, strings that will be inserted as text nodes and in-place definitions of
TagNodeinstances fromtag(). The latter will be assigned to the same namespace.
- Returns:
The newly created tag node.
- static parse(text: AnyStr, parser_options: ParserOptions | None = None) TagNode[source]¶
Parses the given string or bytes sequence into a new tree.
- Parameters:
text – A serialized XML tree.
parser_options – A
delb.ParserOptionsclass to configure the used parser.
- prepend_children(*node: delb.NodeBase, clone: bool = False) tuple[delb.NodeBase, ...][source]¶
Adds one or more nodes as child nodes before any existing to the child nodes of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were prepended.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- replace_with(node: delb.typing.NodeSource, clone: bool = False) delb.NodeBase¶
Removes the node and places the given one in its tree location.
The node can be a concrete instance of any node type or a rather abstract description in the form of a string or an object returned from the
tag()function that is used to derive aTextNoderespectivelyTagNodeinstance from.- Parameters:
node – The replacing node.
clone – A concrete, replacing node is cloned if
True.
- Returns:
The removed node.
- serialize(*, format_options: FormatOptions | None = None, namespaces: delb.typing.NamespaceDeclarations | None = None, newline: str | None = None)[source]¶
Returns a string that contains the serialization of the node. See Serialization for details.
- Parameters:
format_options – An instance of
FormatOptionscan be provided to configure formatting.namespaces – A mapping of prefixes to namespaces. These are overriding possible declarations from a parsed serialisat that the document instance stems from. Prefixes for undeclared namespaces are enumerated with the prefix
ns.newline – See
io.TextIOWrapperfor a detailed explanation of the parameter with the same name.
- property universal_name: str¶
The node’s qualified name in Clark notation.
- xpath(expression: str, namespaces: delb.typing.NamespaceDeclarations | None = None) QueryResults[source]¶
Queries the tree with an XPath expression with this node as initial context node.
- Parameters:
expression – A supported XPath 1.0 expression that contains one or more location paths.
namespaces – A mapping of prefixes that are used in the expression to namespaces. The declarations that were used in a document’s source serialisat serve as fallback.
- Returns:
All nodes that match the evaluation of the provided XPath expression.
See Queries with XPath & CSS for details on the extent of the XPath implementation.
Tag attribute¶
- class delb.nodes.Attribute(attributes: TagAttributes, qualified_name: tuple[str, str])[source]¶
Attribute objects represent tag node’s attributes. See the
delb.TagNode.attributes()documentation for capabilities.- property universal_name: str¶
The attribute’s namespace and local name in Clark notation.
Text¶
- class delb.TextNode(reference_or_text: _Element | str | TextNode, position: int = 0)[source]¶
TextNodes contain the textual data of a document. The class shall not be initialized by client code, just throw strings into the trees.
Instances expose all methods of
strexceptstr.index():>>> node = TextNode("Show us the way to the next whisky bar.") >>> node.split() ['Show', 'us', 'the', 'way', 'to', 'the', 'next', 'whisky', 'bar.']
Instances can be tested for inequality with other text nodes and strings:
>>> TextNode("ham") == TextNode("spam") False >>> TextNode("Patsy") == "Patsy" True
And they can be tested for substrings:
>>> "Sir" in TextNode("Sir Bedevere the Wise") True
Attributes that rely to child nodes yield nothing respectively
None.Node properties:
The depth (or level) of the node in its tree.
The node's index within the parent's collection of child nodes or
Nonewhen the node has no parent.The prefix to namespace mapping of the node.
Node content properties:
The node's text content.
The concatenated contents of all text node descendants in document order.
Related document and nodes properties:
The
Documentinstance that the node is associated with orNone.The node's first child.
The node's last child node.
The node's last descendant.
The node's parent or
None.Methods to query the tree:
Queries the tree with an XPath expression with this node as initial context node.
Methods to fetch a relative node:
Retrieves the next filter matching node on the following axis.
Retrieves the next filter matching node on the following-sibling axis.
Retrieves the next filter matching node on the preceding axis.
Retrieves the next filter matching node on the preceding-sibling axis.
Methods to iterate over related node:
Iterator over the filter matching nodes on the ancestor axis.
A generator iterator that yields nothing.
A generator iterator that yields nothing.
Iterator over the filter matching nodes on the following axis.
Iterator over the filter matching nodes on the following-sibling axis.
Iterator over the filter matching nodes on the preceding axis.
Iterator over the filter matching nodes on the preceding-sibling axis.
Methods to add nodes to a tree:
Adds one or more nodes to the right of the node this method is called on.
Adds one or more nodes to the left of the node this method is called on.
Methods to remove a node:
Removes the node from its tree.
Removes the node and places the given one in its tree location.
Methods:
Creates a new node of the same type with duplicated contents.
Creates a new
TagNodeinstance in the node's context.Returns a string that contains the serialization of the node.
- add_following_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the right of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- add_preceding_siblings(*node: delb.typing.NodeSource, clone: bool = False) tuple[delb.NodeBase, ...]¶
Adds one or more nodes to the left of the node this method is called on.
- Parameters:
node – The node(s) to be added.
clone – Clones the concrete nodes before adding if
True.
- Returns:
The concrete nodes that were added.
The nodes can be concrete instances of any node type or rather abstract descriptions in the form of strings or objects returned from the
tag()function that are used to deriveTextNoderespectivelyTagNodeinstances from.
- clone(deep: bool = False, quick_and_unsafe: bool | None = None) delb.NodeBase[source]¶
Creates a new node of the same type with duplicated contents.
- Parameters:
deep – Clones the whole subtree if
True.quick_and_unsafe – Deprecated.
- Returns:
A copy of the node.
- detach(retain_child_nodes: bool = False) TextNode[source]¶
Removes the node from its tree.
- Parameters:
retain_child_nodes – Keeps the node’s descendants in the originating tree if
True.- Returns:
The removed node.
- fetch_following(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following axis.
- fetch_following_sibling(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the following-sibling axis.
- fetch_preceding(*filter: delb.typing.Filter) delb.NodeBase | None¶
Retrieves the next filter matching node on the preceding axis.
- fetch_preceding_sibling(*filter: delb.typing.Filter) delb.NodeBase | None[source]¶
Retrieves the next filter matching node on the preceding-sibling axis.
- first_child = None¶
The node’s first child.
- property index: int | None¶
The node’s index within the parent’s collection of child nodes or
Nonewhen the node has no parent.
- iterate_ancestors(*filter: delb.typing.Filter) Iterator[TagNode]¶
Iterator over the filter matching nodes on the ancestor axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the ancestor nodes from bottom to top.
- iterate_children(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
A generator iterator that yields nothing.
- iterate_descendants(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
A generator iterator that yields nothing.
- iterate_following(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the following nodes in document order.
- iterate_following_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the following-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s right.
- iterate_preceding(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the previous nodes in document order.
- iterate_preceding_siblings(*filter: delb.typing.Filter) Iterator[delb.NodeBase]¶
Iterator over the filter matching nodes on the preceding-sibling axis.
- Parameters:
filter – Any number of filter s that a node must match to be yielded.
- Returns:
A generator iterator that yields the siblings to the node’s left.
- last_child = None¶
The node’s last child node.
- last_descendant = None¶
The node’s last descendant.
- new_tag_node(local_name: str, attributes: dict[AttributeAccessor, str] | None = None, namespace: str | None = None, children: Sequence[str | NodeBase | _TagDefinition] = ()) TagNode¶
Creates a new
TagNodeinstance in the node’s context.- Parameters:
local_name – The tag name.
attributes – Optional attributes that are assigned to the new node.
namespace – An optional tag namespace. If none is provided, the context node’s namespace is inherited.
children – An optional sequence of objects that will be appended as child nodes. This can be existing nodes, strings that will be inserted as text nodes and in-place definitions of
TagNodeinstances fromtag(). The latter will be assigned to the same namespace.
- Returns:
The newly created tag node.
- replace_with(node: delb.typing.NodeSource, clone: bool = False) delb.NodeBase¶
Removes the node and places the given one in its tree location.
The node can be a concrete instance of any node type or a rather abstract description in the form of a string or an object returned from the
tag()function that is used to derive aTextNoderespectivelyTagNodeinstance from.- Parameters:
node – The replacing node.
clone – A concrete, replacing node is cloned if
True.
- Returns:
The removed node.
- serialize(*, format_options: FormatOptions | None = None, namespaces: delb.typing.NamespaceDeclarations | None = None, newline: str | None = None)¶
Returns a string that contains the serialization of the node. See Serialization for details.
- Parameters:
format_options – An instance of
FormatOptionscan be provided to configure formatting.namespaces – A mapping of prefixes to namespaces. These are overriding possible declarations from a parsed serialisat that the document instance stems from. Prefixes for undeclared namespaces are enumerated with the prefix
ns.newline – See
io.TextIOWrapperfor a detailed explanation of the parameter with the same name.
- xpath(expression: str, namespaces: delb.typing.NamespaceDeclarations | None = None) QueryResults¶
Queries the tree with an XPath expression with this node as initial context node.
- Parameters:
expression – A supported XPath 1.0 expression that contains one or more location paths.
namespaces – A mapping of prefixes that are used in the expression to namespaces. The declarations that were used in a document’s source serialisat serve as fallback.
- Returns:
All nodes that match the evaluation of the provided XPath expression.
See Queries with XPath & CSS for details on the extent of the XPath implementation.
Node constructors¶
- delb.new_comment_node(content: str) CommentNode[source]¶
Creates a new
CommentNode.- Parameters:
content – The comment’s content a.k.a. text.
- Returns:
The newly created comment node.
- delb.new_processing_instruction_node(target: str, content: str) ProcessingInstructionNode[source]¶
Creates a new
ProcessingInstructionNode.- Parameters:
target – The processing instruction’s target name.
content – The processing instruction’s text.
- Returns:
The newly created processing instruction node.
- delb.new_tag_node(local_name: str, attributes: dict[AttributeAccessor, str] | TagAttributes | None = None, namespace: str | None = None, children: Iterable[NodeSource] = ()) TagNode[source]¶
Creates a new
TagNodeinstance outside any context. It is preferable to use the methodnew_tag_nodeon instances of documents and nodes where the namespace is inherited.- Parameters:
local_name – The tag name.
attributes – Optional attributes that are assigned to the new node.
namespace – An optional tag namespace.
children – An optional iterable of objects that will be appended as child nodes. This can be existing nodes, strings that will be inserted as text nodes and in-place definitions of
TagNodeinstances fromtag(). The latter will be assigned to the same namespace.
- Returns:
The newly created tag node.
Filters¶
The following implementations are shipped with the library. Also see
delb.typing.Filter
Default filters¶
- delb.altered_default_filters(*filter: delb.typing.Filter, extend: bool = False)[source]¶
This function can be either used as as context manager or decorator to define a set of
default_filtersfor the encapsuled code block or callable.- Parameters:
filter – The filters to set or append.
extend – Extends the currently active filters with the given ones instead of replacing them.
These are then applied in all operations that allow node filtering, like
TagNode.next_node(). Mind that they also affect a node’s index property and indexed access to child nodes.>>> root = Document( ... '<root xmlns="foo"><a/><!--x--><b/><!--y--><c/></root>' ... ).root >>> with altered_default_filters(is_comment_node): ... print([x.content for x in root.iterate_children()]) ['x', 'y']
As the default filters shadow comments and processing instructions by default, use no argument to unset this in order to access all type of nodes.
Contributed filters¶
- delb.any_of(*filter: delb.typing.Filter) delb.typing.Filter[source]¶
A node filter wrapper that matches when any of the given filters is matching, like a boolean
or.
- delb.is_comment_node(node: delb.NodeBase) bool[source]¶
A node filter that matches
CommentNodeinstances.
- delb.is_processing_instruction_node(node: delb.NodeBase) bool[source]¶
A node filter that matches
ProcessingInstructionNodeinstances.
- delb.is_tag_node(node: delb.NodeBase) bool[source]¶
A node filter that matches
TagNodeinstances.
- delb.is_text_node(node: delb.NodeBase) bool[source]¶
A node filter that matches
TextNodeinstances.