Expand description
§An XSLT compiler
Compile an XSLT stylesheet into a Transformation.
Once the stylesheet has been compiled, it may then be evaluated with an appropriate context.
NB. This module, by default, does not resolve include or import statements. See the xrust-net crate for a helper module to do that.
use std::rc::Rc;
use xrust::xdmerror::{Error, ErrorKind};
use xrust::qname::QualifiedName;
use xrust::item::{Item, Node, NodeType, Sequence, SequenceTrait};
use xrust::transform::Transform;
use xrust::transform::context::{StaticContext, StaticContextBuilder};
use xrust::trees::smite::RNode;
use xrust::parser::xml::parse;
use xrust::xslt::from_document;
// A little helper function to parse an XML document
fn make_from_str(s: &str) -> Result<RNode, Error> {
let doc = RNode::new_document();
let e = parse(doc.clone(), s, None)?;
Ok(doc)
}
// The source document (a tree)
let src = Item::Node(
make_from_str("<Example><Title>XSLT in Rust</Title><Paragraph>A simple document.</Paragraph></Example>")
.expect("unable to parse XML")
);
// The XSL stylesheet
let style = make_from_str("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='child::Example'><html><xsl:apply-templates/></html></xsl:template>
<xsl:template match='child::Title'><head><title><xsl:apply-templates/></title></head></xsl:template>
<xsl:template match='child::Paragraph'><body><p><xsl:apply-templates/></p></body></xsl:template>
</xsl:stylesheet>")
.expect("unable to parse stylesheet");
// Create a static context (with dummy callbacks)
let mut static_context = StaticContextBuilder::new()
.message(|_| Ok(()))
.fetcher(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
.parser(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
.build();
// Compile the stylesheet
let mut ctxt = from_document(
style,
None,
make_from_str,
|_| Ok(String::new())
).expect("failed to compile stylesheet");
// Set the source document as the context item
ctxt.context(vec![src], 0);
// Make an empty result document
ctxt.result_document(RNode::new_document());
// Let 'er rip!
// Evaluate the transformation
let seq = ctxt.evaluate(&mut static_context)
.expect("evaluation failed");
// Serialise the sequence as XML
assert_eq!(seq.to_xml(), "<html><head><title>XSLT in Rust</title></head><body><p>A simple document.</p></body></html>")
Traits§
- XSLT
- The XSLT trait allows an object to use an XSL Stylesheet to transform a document into a Sequence.
Functions§
- from_
document - Compiles a Node into a transformation Context. NB. Due to whitespace stripping, this is destructive of the stylesheet. The argument f is a closure that parses a string to a Node. The argument g is a closure that resolves a URL to a string. These are used for include and import modules. They are not included in this module since some environments, in particular Wasm, do not have I/O facilities.
- strip_
source_ document - Strip whitespace nodes from a XDM tree. This function operates under the direction of the xsl:strip-space and xsl:preserve-space directives in a XSLT stylesheet.
- strip_
whitespace - Strip whitespace nodes from a XDM tree. See XSLT 4.3. The Node argument must be the document node of the tree.