Metreeca Gear
    Preparing search index...

    Function xpath

    Creates an XPath selector.

    • Creates an XPath selector task.

      The generated task converts a feed of parsed X/HTML trees into a feed of XPath selectors, one selector per tree, so that a consumer addresses what a tree holds by expression rather than walking it.

      Note

      • Incremental: each selector is emitted as soon as its tree is drawn, so the feed produced runs dry as the feed drawn from does and an endless source is read as long as it is consumed.
      • Streaming: trees are drawn one at a time and none retained, so the length of the feed weighs on memory no more than a single tree does; a selector keeps the tree it targets for as long as a consumer holds it.
      • Stateless: every tree is targeted on its own, so the outcome is unaffected by how the feed is split across nested feeds or runs.

      Returns Task<AnyNode, XPath>

      A task converting a feed of parsed X/HTML trees into a feed of XPath selectors

      Error While the feed is consumed, whatever the source reports while producing trees

    • Creates an XPath mapping task.

      The generated task converts a feed of parsed X/HTML trees into a feed of mapped results, one result per tree, so that a consumer works on the shape it is after rather than on the one the markup states.

      Note

      • Incremental: each result is emitted as soon as its tree is drawn, so the feed produced runs dry as the feed drawn from does and an endless source is read as long as it is consumed.
      • Streaming: trees are drawn one at a time and released as soon as their result is assembled, so the length of the feed weighs on memory no more than a single tree does.
      • Stateless: every tree is mapped on its own, so the outcome is unaffected by how the feed is split across nested feeds or runs.

      Type Parameters

      • V

        The type of the result mapped from each incoming tree

      Parameters

      • mapper: (path: XPath) => V

        The mapping function, applied to an XPath selector targeting the tree being processed

      Returns Task<AnyNode, V>

      A task converting a feed of parsed X/HTML trees into a feed of mapped results

      Error While the feed is consumed, whatever the source reports while producing trees, or whatever mapper reports while mapping a tree, including a SyntaxError for a malformed expression

      const events = xpath(path => ({

      title: path("//h1").map(string),
      links: path("//a/@href").map(link)

      }));
    • Creates an XPath selector over given nodes.

      Targets nodes already at hand, outside a feed, so that a consumer addressing a node it holds does so exactly as one addressing a tree drawn from a source.

      Important

      A call with no node, spreading an empty list included, creates a task over a feed of trees rather than a selector with an empty target set.

      Parameters

      • ...nodes: readonly Target[]

        The target nodes, in the order they are to be selected from

      Returns XPath

      An immutable selector targeting nodes