[][src]Struct sxd_xpath_visitor::context::Context

pub struct Context<'d> { /* fields omitted */ }

Contains the context in which XPath expressions are executed. The context contains functions, variables, and namespace mappings.

Examples

A complete example showing all optional settings.

use std::collections::HashMap;
use sxd_document::parser;
use sxd_xpath::{Factory, Context, Value};
use sxd_xpath::{context, function};

struct Sigmoid;
impl function::Function for Sigmoid {
    fn evaluate<'c, 'd>(&self,
                        _context: &context::Evaluation<'c, 'd>,
                        args: Vec<Value<'d>>)
                        -> Result<Value<'d>, function::Error>
    {
        let mut args = function::Args(args);
        args.exactly(1)?;
        let val = args.pop_number()?;

        let computed = (1.0 + (-val).exp()).recip();

        Ok(Value::Number(computed))
    }
}

fn main() {
    let package = parser::parse("<thing xmlns:ns0='net:brain' ns0:bonus='1' />")
        .expect("failed to parse XML");
    let document = package.as_document();
    let node = document.root().children()[0];

    let mut context = Context::new();
    context.set_function("sigmoid", Sigmoid);
    context.set_variable("t", 2.0);
    context.set_namespace("neural", "net:brain");

    let xpath = "sigmoid(@neural:bonus + $t)";

    let factory = Factory::new();
    let xpath = factory.build(xpath).expect("Could not compile XPath");

    let value = xpath.evaluate(&context, node).expect("XPath evaluation failed");

    assert_eq!(0.952, (value.number() * 1000.0).trunc() / 1000.0);
}

Note that we are using a custom function (sigmoid), a variable ($t), and a namespace (neural:). The current node is passed to the evaluate method and is not the root of the tree but the top-most element.

Implementations

impl<'d> Context<'d>[src]

pub fn new() -> Self[src]

Registers the core XPath 1.0 functions.

pub fn without_core_functions() -> Self[src]

No functions, variables or namespaces will be defined.

pub fn set_function<N, F>(&mut self, name: N, function: F) where
    N: Into<OwnedQName>,
    F: Function + 'static, 
[src]

Register a function within the context

pub fn set_variable<N, V>(&mut self, name: N, value: V) where
    N: Into<OwnedQName>,
    V: Into<Value<'d>>, 
[src]

Register a variable within the context

pub fn set_namespace(&mut self, prefix: &str, uri: &str)[src]

Register a namespace prefix within the context

Trait Implementations

impl<'d> Default for Context<'d>[src]

Auto Trait Implementations

impl<'d> !RefUnwindSafe for Context<'d>[src]

impl<'d> !Send for Context<'d>[src]

impl<'d> !Sync for Context<'d>[src]

impl<'d> Unpin for Context<'d>[src]

impl<'d> !UnwindSafe for Context<'d>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Downcast for T where
    T: Any
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.