Module xmlrat_xpath

Compile and execute XPath 1.0 expressions at runtime.

Description

Compile and execute XPath 1.0 expressions at runtime.

This module provides support for compiling a subset of XPath 1.0 to Erlang code. The generated code consists of successive pattern matches and list comprehensions, plus some calls to runtime helpers as needed for predicates with position matches in them.

The subset of XPath 1.0 implemented is chosen to be as "safe" as possible -- it should avoid any accidental exponential expansion in memory usage or time, and compiles to simple pattern matches as much as possible. Being a fully compliant XPath implementation is a non-goal.

It's important to note that every XPath expression this module compiles will result in a new Erlang module being loaded by the code server. These are a finite resource, so you should avoid dynamically compiling an arbitray number of input expressions (e.g. don't accept XPath expressions from user input).

Expressions support being parametrised by using variables (see the type varbinds()).

Supported XPath 1.0 syntax features:

Data Types

compile_options()

compile_options() = #{namespaces => nsmap()}

nsmap()

nsmap() = #{xmlrat:nsname() => xmlrat:uri()}

Map of namespaces used in the XPath expression itself (does not have to match the namespace set used by the document).

run_options()

run_options() = #{namespaces => nsmap(), variables => varbinds()}

Options used with run/3.

varbinds()

varbinds() = #{binary() => binary() | integer()}

Bound variables for use within an XPath expression.

For example, after compiling the XPath expression /foo[@bar = $var], you could execute it with different values in place of $var by using:

xmlrat_xpath:run(Expr, Doc, #{variables =>
  #{<<"var">> => <<"1">>}})
etc.

xpath()

xpath() = string() | binary()

An XPath 1.0 expression.

xpath_ref()

abstract datatype: xpath_ref()

xpath_result()

xpath_result() = [xmlrat:element()] | [xmlrat:attribute()] | binary() | [xmlrat:content()]

Function Index

compile/1Compiles an XPath expression to Erlang code.
compile/2Compiles an XPath expression to Erlang code, with options.
run/2Executes an XPath expression over an input document.
run/3Executes an XPath expression over an input document, with options.

Function Details

compile/1

compile(XPath::xpath()) -> {ok, xpath_ref()} | {error, term()}

Compiles an XPath expression to Erlang code.

Returns an xpath_ref() which can be passed to run().

compile/2

compile(XPath::xpath(), Opts::compile_options()) -> {ok, xpath_ref()} | {error, term()}

Compiles an XPath expression to Erlang code, with options.

run/2

run(Xpath_ref::xpath() | xpath_ref(), Doc::xmlrat:document()) -> {ok, xpath_result()} | {error, term()}

Executes an XPath expression over an input document.

run/3

run(Xpath_ref::xpath() | xpath_ref(), Doc::xmlrat:document(), Opts::run_options()) -> {ok, xpath_result()} | {error, term()}

Executes an XPath expression over an input document, with options.


Generated by EDoc