Context

Struct Context 

Source
pub struct Context<'d> { /* private fields */ }
Expand description

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.

extern crate sxd_document;
extern crate sxd_xpath;

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 xpath = xpath.expect("No XPath was compiled");

    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§

Source§

impl<'d> Context<'d>

Source

pub fn new() -> Self

Registers the core XPath 1.0 functions.

Source

pub fn without_core_functions() -> Self

No functions, variables or namespaces will be defined.

Source

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

Register a function within the context

Source

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

Register a variable within the context

Source

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

Register a namespace prefix within the context

Trait Implementations§

Source§

impl<'d> Default for Context<'d>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'d> Freeze for Context<'d>

§

impl<'d> !RefUnwindSafe for Context<'d>

§

impl<'d> !Send for Context<'d>

§

impl<'d> !Sync for Context<'d>

§

impl<'d> Unpin for Context<'d>

§

impl<'d> !UnwindSafe for Context<'d>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.