Skip to main content

Crate xpath

Crate xpath 

Source
Expand description

XPath 1.0 over a parsec-html Document, written from the W3C recommendation: every axis, node test and function of the core library, with the number, string and boolean rules of sections 3 and 4.

The tree is read as lxml reads an HTML page, which is what scrapers expect: element and attribute names are the parser’s lowercase ones (SVG’s clippath, viewbox) in no namespace, so //div and @href match, //DIV and @HREF don’t, and a prefixed name (svg:rect) matches nothing. An XML document built into the same tree brings its namespaces through Namespaces, and names with a prefix take theirs from the prefixes a query is run with.

A query reads the document through an Index, which its owner builds once per version of the tree. Functions beyond the core library come from the caller through Extensions.

Shadow roots are left out, as document.evaluate leaves them out: the XPath data model has no shadow trees, so the axes follow the node tree and never cross into a host’s shadow root or out of one. A shadow tree is queried on its own by indexing its root as one of Index::build’s roots, where it’s a root node as a template’s contents are: / from inside it is the shadow root, and its host isn’t an ancestor.

Structs§

Args
The arguments of a call to an extension function, worked out when asked for, as the core library’s are.
Doc
The document a query runs over: its tree, the tree’s index, and for an XML document its namespaces.
Index
The index of one version of a tree. Its owner builds it once and keeps it until the tree changes: every query reads it, none builds it.
NoExtensions
No functions beyond the core library.
XPath
A compiled XPath expression. It holds no state of any document, so one can be shared by many threads.
XPathError

Enums§

Item
A node in XPath’s data model: a tree node, or an attribute (which the tree keeps on its element rather than as a node of its own). Sixteen bytes: a node-set can hold every node of a big page, so its name and value are read from the document (Doc::attr_at), not kept here.
Value
What an expression evaluates to. A node-set is in document order, without duplicates.
XValue
A value a caller can bind to a $variable.

Constants§

CDATA
COMMENT
DOCTYPE
DOCUMENT
ELEMENT
The node kinds, as the index stores them: the DOM’s nodeType numbers.
PI
TEXT
WORK_LIMIT
How much work one run may do before it gives up: nodes looked at on axes and by lang(), predicate tests, and nodes and every 16 bytes of text read for string-values. Some queries are quadratic on any engine (//p[following::p]), and a hostile page can make them run for hours. A unit takes one to twenty-odd nanoseconds, so this is a few seconds at most: still over ten times what the slowest query of the benchmark pages needs.
XML_NS
The namespace the xml prefix is always bound to.

Traits§

Extensions
Functions beyond XPath 1.0’s core library, which a caller provides. A core function of the same name wins.
Namespaces
The namespaces of an XML document built into a parsec-html tree, which the tree leaves to its host. Ids are the host’s; 0 is no namespace.