Skip to main content

xpath

Macro xpath 

Source
macro_rules! xpath {
    ($c:expr, $src:literal $(, $name:ident = $val:expr)* $(,)?) => { ... };
    ($c:expr, $src:literal, $ctx:expr $(, $name:ident = $val:expr)* $(,)?) => { ... };
    ($c:expr, $src:literal, $name:ident = $val:expr, $($rest:tt)*) => { ... };
    ($c:expr, $src:literal, $ctx:expr, $second:expr $(, $name:ident = $val:expr)* $(,)?) => { ... };
    ($($rest:tt)*) => { ... };
}
Expand description

Evaluates an XPath expression on a Composer.

xpath!(c, "expr")                             // no context item, no variables
xpath!(c, "expr", node)                       // a context item
xpath!(c, "expr", i = &item, b = &bids)       // the external variables $i and $b
xpath!(c, "expr", items, i = &item)           // a context item, then variables

The result is Result<Value, ComposeError>, so a call site ends in ? and then asks the value for what it wants — nodes, atomics, boolean, string, number, key.

§The argument order, and why it is a rule

The expression is always a string literal. A context item, when there is one, is always the third argument, directly after it; everything after that is a name = value binding, and an optional trailing comma is accepted everywhere.

name = value is also Rust’s assignment expression, so the bindings-only shape is matched before the context shape: xpath!(c, "$node", node = value) binds $node and leaves the context item empty. A named binding never implies a context item, and xpath!(c, "expr", node) never binds anything. A context item after a binding, or two of them, is a compile error naming the expected order; for preparatory work, use a block that ends in the node — xpath!(c, "expr", { prepare(); doc.root() }).

§What may be a context item

Nav, &Nav, Doc or &Doc — anything IntoContextNode accepts. A document supplies its document node, a borrowed navigator is cloned. Binding values are anything IntoXPathValue accepts, which is every atomic Rust scalar, every XDM value, a node, a document, a sequence, and Option of any of those. Variable names are Rust identifiers; a prefixed external variable needs Composer::eval directly.

§Evaluation

Each supplied expression is evaluated exactly once, in the order written: the composer, then the context item, then the binding values from left to right. Every one of them appears exactly once in the expansion, with the composer and the context item held in temporaries of the macro’s own, so no conversion can repeat an evaluation or reorder a side effect.

§Examples

All four call forms:

use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, Composer};
use xsd_schema::namespace::NameTable;
use xsd_schema::xpath;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let items = c.load_str("<items><item n='1'/><item n='2'/></items>")?;

assert_eq!(xpath!(c, "1 + 1")?.string()?, "2");
assert_eq!(xpath!(c, "count(//item)", items)?.string()?, "2");
assert_eq!(xpath!(c, "$n * 2", n = 21)?.string()?, "42");
assert_eq!(
    xpath!(c, "count(//item[@n = $n])", items, n = "2")?.string()?,
    "1",
);

A binding is not a context item:

use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, Composer};
use xsd_schema::namespace::NameTable;
use xsd_schema::xpath;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<items><item/></items>")?;
let node = doc.document_element().expect("a document element");

// `node = …` binds $node …
assert_eq!(xpath!(c, "name($node)", node = &node)?.string()?, "items");
// … and the same value as the context item needs no name.
assert_eq!(xpath!(c, "name()", &node)?.string()?, "items");

Inside a pipeline closure, where most real call sites are:

use xsd_schema::compose::{pipe, ComposeError, Composer};
use xsd_schema::document::SerializeOptions;
use xsd_schema::namespace::NameTable;
use xsd_schema::{form, xpath};
use bumpalo::Bump;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let stock = c.load_str("<stock><part qty='7'>bolt</part><part qty='0'>nut</part></stock>")?;

let rows = pipe::nodes(xpath!(c, "//part", stock)?)
    .try_filter(|p| xpath!(c, "@qty > 0", p)?.boolean())
    .try_map(|p| Ok(form! { (part ^{ xpath!(c, "string()", &p)? }) }));

assert_eq!(
    c.build(form! { (in_stock ..?^{ rows }) })?.to_xml(&SerializeOptions::default())?,
    "<in_stock><part>bolt</part></in_stock>",
);

A context item after a binding does not compile:

ⓘ
use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, Composer};
use xsd_schema::namespace::NameTable;
use xsd_schema::xpath;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b/></a>")?;
let node = doc.root();

// The context item belongs before the bindings.
let wrong = xpath!(c, "name($x)", x = &node, node)?;

Neither does a second context item:

ⓘ
use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, Composer};
use xsd_schema::namespace::NameTable;
use xsd_schema::xpath;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b/></a>")?;

// One context item only; bind the other value by name.
let wrong = xpath!(c, "name()", doc.root(), doc.root())?;