Queries with XPath & CSS¶
delb allows querying of nodes with CSS selector and XPath expressions. CSS selectors are converted to XPath expressions with a third-party library before evaluation and they are only supported as far as their computed XPath equivalents are supported by delb’s very own XPath implementation.
This implementation is not fully compliant with one of the W3C’s XPath specifications. It mostly covers the XPath 1.0 specs, but focuses on the querying via path expressions with simple constraints while it omits a broad employment of computations (that’s what programming languages are for) and has therefore these intended deviations from that standard:
Default namespaces can be addressed in node and attribute names, by simply using no prefix.
The attribute and namespace axes are not supported in location steps (see also below).
In predicates only the attribute axis can be used in its abbreviated form (
@name).Path evaluations within predicates are not available.
- Only these predicate functions are provided and tested:
booleanconcatcontainslastnotpositionstarts-withtextBehaves as if deployed as a single step location path that only tests for the node type text. Hence it returns the contents of the context node’s first child node that is a text node or an empty string when there is none.
Please refrain from extension requests without a proper, concrete implementation proposal.
If you’re accustomed to retrieve attribute values with XPath expressions, employ the functionality of the higher programming language at hand like this:
>>> [x.attributes["target"] for x in root.xpath("//foo")
... if "target" in x.attributes ]
Instead of:
>>> root.xpath("//foo/@target")
See _delb.plugins.PluginManager.register_xpath_function() regarding the use of
custom functions.
- class _delb.xpath.EvaluationContext(node: NodeBase, position: int, size: int, namespaces: Namespaces)[source]¶
Instances of this class are passed to XPath functions in order to pass contextual information.
- namespaces: Namespaces¶
A mapping of prefixes to namespaces that is used in the whole evaluation.
- class _delb.xpath.QueryResults(results: Iterable[delb.NodeBase])[source]¶
A container with the the results of a CSS selector or XPath query with some helpers for better readable Python expressions.
- as_list() list[delb.NodeBase][source]¶
The contained nodes as a new
list.
- property as_tuple: tuple[delb.NodeBase, ...]¶
The contained nodes in a
tuple.
- filtered_by(*filters: delb.typing.Filter) QueryResults[source]¶
Returns another
QueryResultsinstance that contains all nodes filtered by the provided filter s.
- property first: delb.NodeBase | None¶
The first node from the results or
Noneif there are none.
- in_document_order() QueryResults[source]¶
Returns another
QueryResultsinstance where the contained nodes are sorted in document order.
- property last: delb.NodeBase | None¶
The last node from the results or
Noneif there are none.