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>
impl<N: Node> Context<N>
pub fn new() -> Self
sourcepub fn result_document(&mut self, rd: N)
pub fn result_document(&mut self, rd: N)
Sets the result document. Any nodes created by the transformation are owned by this document.
sourcepub fn evaluate<F: FnMut(&str) -> Result<(), Error>>(
&self,
stctxt: &mut StaticContext<F>
) -> Result<Sequence<N>, Error>
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")
sourcepub fn find_templates<F: FnMut(&str) -> Result<(), Error>>(
&self,
stctxt: &mut StaticContext<F>,
i: &Rc<Item<N>>
) -> Result<Vec<Rc<Template<N>>>, Error>
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
sourcepub fn dispatch<F: FnMut(&str) -> Result<(), Error>>(
&self,
stctxt: &mut StaticContext<F>,
t: &Transform<N>
) -> Result<Sequence<N>, Error>
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§
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>where
N: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more