[−][src]Struct sxd_xpath_visitor::context::Context
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]
N: Into<OwnedQName>,
F: Function + 'static,
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]
N: Into<OwnedQName>,
V: Into<Value<'d>>,
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
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Downcast for T where
T: Any,
[src]
T: Any,
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
[src]
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
[src]
pub fn as_any(&self) -> &(dyn Any + 'static)
[src]
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,