Struct Context

Source
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 previous_context(&mut self, i: Item<N>)

Sets the “current” 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 declare_key(&mut self, name: String, m: Pattern<N>, u: Transform<N>)

Declare a key

Source

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

Source

pub fn dump_key_values(&self)

Source

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

Source

pub fn callable_push(&mut self, qn: QualifiedName, c: Callable<N>)

Callable components: named templates and user-defined functions

Source

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")
Source

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.

Source

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>")

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>

Derive a new Context from an old Context. The context item in the old context becomes the “current” item in the new context.

Source§

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

Converts to this type from the input type.
Source§

impl<N: Node> From<Vec<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> Freeze for Context<N>
where N: Freeze,

§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,