pub struct Context<N: Node> { /* private fields */ }
Expand description

The transformation context. This is the dynamic context. The static parts of the context are in a separate structure. Contexts are immutable, but frequently are cloned to provide a new context. Although it is optional, it would be very unusual not to set a result document in a context since nodes cannot be created in the result without one.

Implementations§

source§

impl<N: Node> Context<N>

source

pub fn new() -> Self

source

pub fn context(&mut self, s: Sequence<N>, i: usize)

Sets the context item.

source

pub fn result_document(&mut self, rd: N)

Sets the result document. Any nodes created by the transformation are owned by this document.

source

pub fn evaluate<F: FnMut(&str) -> Result<(), Error>>( &self, stctxt: &mut StaticContext<F> ) -> Result<Sequence<N>, Error>

Evaluate finds a template matching the current item and evaluates the body of the template, returning the resulting Sequence.

use std::rc::Rc;
use xrust::xdmerror::Error;
use xrust::item::{Item, Sequence, SequenceTrait, Node, NodeType};
use xrust::transform::Transform;
use xrust::transform::context::{Context, StaticContext};
use xrust::trees::intmuttree::{Document, RNode, NodeBuilder};
use xrust::xslt::from_document;

// This is necessary since there is no callback defined for the static context
type F = Box<dyn FnMut(&str) -> Result<(), Error>>;

// A little helper function to parse a string to a Document Node
fn make_from_str(s: &str) -> RNode {
  let e = Document::try_from((s, None, None))
    .expect("failed to parse XML")
    .content[0].clone();
  let mut d = NodeBuilder::new(NodeType::Document).build();
  d.push(e).expect("unable to append node");
  d
}

let sd = Rc::new(Item::Node(make_from_str("<Example/>")));
let style = make_from_str("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'><xsl:apply-templates/></xsl:template>
<xsl:template match='child::Example'>This template will match</xsl:template>
</xsl:stylesheet>");
let mut context = from_document(style, None, |s| Ok(make_from_str(s)), |_| Ok(String::new())).expect("unable to compile stylesheet");
context.context(vec![sd], 0);
context.result_document(make_from_str("<Result/>"));
let sequence = context.evaluate(&mut StaticContext::<F>::new()).expect("evaluation failed");
assert_eq!(sequence.to_string(), "This template will match")
source

pub fn find_templates<F: FnMut(&str) -> Result<(), Error>>( &self, stctxt: &mut StaticContext<F>, i: &Rc<Item<N>> ) -> Result<Vec<Rc<Template<N>>>, Error>

Find a template with a matching Pattern

source

pub fn dispatch<F: FnMut(&str) -> Result<(), Error>>( &self, stctxt: &mut StaticContext<F>, t: &Transform<N> ) -> Result<Sequence<N>, Error>

Interpret the given Transform object

use std::rc::Rc;
use xrust::xdmerror::Error;
use xrust::item::{Item, Sequence, SequenceTrait, Node, NodeType};
use xrust::transform::{Transform, NodeMatch, NodeTest, KindTest,  Axis};
use xrust::transform::context::{Context, ContextBuilder, StaticContext};
use xrust::trees::intmuttree::{Document, RNode, NodeBuilder};

// This is necessary since there is no callback defined for the static context
type F = Box<dyn FnMut(&str) -> Result<(), Error>>;

// A little helper function to parse a string to a Document Node
fn make_from_str(s: &str) -> RNode {
  let e = Document::try_from((s, None, None))
    .expect("failed to parse XML")
    .content[0].clone();
  let mut d = NodeBuilder::new(NodeType::Document).build();
  d.push(e).expect("unable to append node");
  d
}

// Equivalent to "child::*"
let t = Transform::Step(NodeMatch {axis: Axis::Child, nodetest: NodeTest::Kind(KindTest::Any)});
let sd = Rc::new(Item::Node(make_from_str("<Example/>")));
let context = ContextBuilder::new()
  .current(vec![sd])
  .build();
let sequence = context.dispatch(&mut StaticContext::<F>::new(), &t).expect("evaluation failed");
assert_eq!(sequence.to_xml(), "<Example></Example>")

Trait Implementations§

source§

impl<N: Clone + Node> Clone for Context<N>

source§

fn clone(&self) -> Context<N>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<N: Debug + Node> Debug for Context<N>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<N: Node> From<&Context<N>> for ContextBuilder<N>

source§

fn from(c: &Context<N>) -> Self

Converts to this type from the input type.
source§

impl<N: Node> From<Vec<Rc<Item<N>>>> for Context<N>

source§

fn from(value: Sequence<N>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<N> RefUnwindSafe for Context<N>
where N: RefUnwindSafe,

§

impl<N> !Send for Context<N>

§

impl<N> !Sync for Context<N>

§

impl<N> Unpin for Context<N>
where N: Unpin,

§

impl<N> UnwindSafe for Context<N>

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.