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 previous_context(&mut self, i: Item<N>)
pub fn previous_context(&mut self, i: Item<N>)
Sets the “current” item.
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 declare_key(&mut self, name: String, m: Pattern<N>, u: Transform<N>)
pub fn declare_key(&mut self, name: String, m: Pattern<N>, u: Transform<N>)
Declare a key
Sourcepub fn populate_key_values<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>(
&mut self,
stctxt: &mut StaticContext<N, F, G, H>,
sd: N,
) -> Result<(), Error>
pub fn populate_key_values<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>( &mut self, stctxt: &mut StaticContext<N, F, G, H>, sd: N, ) -> Result<(), Error>
Calculate the key values for a source document
pub fn dump_key_values(&self)
Sourcepub fn attribute_set(&mut self, _name: QualifiedName, _body: Vec<Transform<N>>)
pub fn attribute_set(&mut self, _name: QualifiedName, _body: Vec<Transform<N>>)
Add a named attribute set. This replaces any previously declared attribute set with the same name
Sourcepub fn callable_push(&mut self, qn: QualifiedName, c: Callable<N>)
pub fn callable_push(&mut self, qn: QualifiedName, c: Callable<N>)
Callable components: named templates and user-defined functions
Sourcepub fn evaluate<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>(
&self,
stctxt: &mut StaticContext<N, F, G, H>,
) -> Result<Sequence<N>, Error>
pub fn evaluate<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>( &self, stctxt: &mut StaticContext<N, F, G, H>, ) -> 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 url::Url;
use xrust::ErrorKind;
use xrust::xdmerror::Error;
use xrust::item::{Item, Sequence, SequenceTrait, Node, NodeType};
use xrust::transform::Transform;
use xrust::transform::context::{Context, StaticContext, StaticContextBuilder};
use xrust::trees::smite::RNode;
use xrust::parser::xml::parse;
use xrust::xslt::from_document;
// A little helper function to parse a string to a Document Node
fn make_from_str(s: &str) -> RNode {
let mut d = RNode::new_document();
parse(d.clone(), s, None)
.expect("failed to parse XML");
d
}
let sd = 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 stctxt = StaticContextBuilder::new()
.message(|_| Ok(()))
.fetcher(|_| Ok(String::new()))
.parser(|s| Ok(make_from_str(s)))
.build();
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 stctxt).expect("evaluation failed");
assert_eq!(sequence.to_string(), "This template will match")
Sourcepub fn find_templates<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>(
&self,
stctxt: &mut StaticContext<N, F, G, H>,
i: &Item<N>,
m: &Option<Rc<QualifiedName>>,
) -> Result<Vec<Rc<Template<N>>>, Error>
pub fn find_templates<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>( &self, stctxt: &mut StaticContext<N, F, G, H>, i: &Item<N>, m: &Option<Rc<QualifiedName>>, ) -> Result<Vec<Rc<Template<N>>>, Error>
Find a template with a matching Pattern in the given mode.
Sourcepub fn dispatch<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>(
&self,
stctxt: &mut StaticContext<N, F, G, H>,
t: &Transform<N>,
) -> Result<Sequence<N>, Error>
pub fn dispatch<F: FnMut(&str) -> Result<(), Error>, G: FnMut(&str) -> Result<N, Error>, H: FnMut(&Url) -> Result<String, Error>>( &self, stctxt: &mut StaticContext<N, F, G, H>, t: &Transform<N>, ) -> Result<Sequence<N>, Error>
Interpret the given Transform object
use std::rc::Rc;
use url::Url;
use xrust::xdmerror::{Error, ErrorKind};
use xrust::item::{Item, Sequence, SequenceTrait, Node, NodeType};
use xrust::transform::{Transform, NodeMatch, NodeTest, KindTest, Axis};
use xrust::transform::context::{Context, ContextBuilder, StaticContext, StaticContextBuilder};
use xrust::trees::smite::RNode;
use xrust::parser::xml::parse;
// A little helper function to parse a string to a Document Node
fn make_from_str(s: &str) -> RNode {
let mut d = RNode::new_document();
parse(d.clone(), s, None)
.expect("failed to parse XML");
d
}
// Equivalent to "child::*"
let t = Transform::Step(NodeMatch {axis: Axis::Child, nodetest: NodeTest::Kind(KindTest::Any)});
let sd = Item::Node(make_from_str("<Example/>"));
let mut stctxt = StaticContextBuilder::new()
.message(|_| Ok(()))
.fetcher(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
.parser(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
.build();
let context = ContextBuilder::new()
.context(vec![sd])
.build();
let sequence = context.dispatch(&mut stctxt, &t).expect("evaluation failed");
assert_eq!(sequence.to_xml(), "<Example></Example>")