Skip to main content

Crate einvoice_xslt_exec

Crate einvoice_xslt_exec 

Source
Expand description

Executor of the XSLT 2.0 subset that the official e-invoicing validation stylesheets use (Schematron compiled to XSLT), with the behaviour of the reference engine, Saxon-HE 12.10: same template rules chosen, same result.

A stylesheet loaded by einvoice_xslt::load is prepared once into an Executable, which then runs on any number of documents with Executable::transform. Preparing compiles every XPath expression with einvoice_xpath_eval, resolves variables, functions, named templates and modes, and indexes the template rules of each mode by the kind and name of the nodes they can match. A transformation parses the document, applies templates to its document node in the default mode and writes the result straight to XML as it is produced (the result tree is never held in memory): for a compiled Schematron, the SVRL report. Written by Executable::transform_compact, the result is kept as a CompactOutput, each distinct piece of its text once, and a ResultObserver reads it as it is written, as a parser of the text would: the report of a large document in a fraction of its size, and read without parsing it again.

Supported, as XSLT 2.0 and Saxon define them:

  • template rules: match patterns (/, root(), path patterns with predicates, //, the XSLT 3.0 parenthesized patterns as Saxon-HE 12.10 reads them: (A | B) alone is A | B, a path ending with (A | B) is a union, (A | B)[p]/c and (a | b)/(c | d)/e match as written, see einvoice_xpath::parse_pattern), modes (named, #default, #all, #current), explicit and default priorities (each alternative of a pattern without explicit priority is a rule of its own, a pattern with one is a single rule), the rule chosen, and the next one for xsl:next-match, exactly as Saxon chooses them (its rule chains, ties going to the last declared rule with an XTDE0540 warning), the built-in rules for every node kind, and for atomic values, which Saxon, an XSLT 3.0 processor, lets xsl:apply-templates select and writes as text;
  • dynamic errors raised while matching a pattern (a template rule’s, or the count pattern of xsl:number), in its predicates or in a global variable or function they use: as in Saxon, the node does not match and the error is a warning, except a circular global variable (XTDE0640), which ends the transformation; a global variable whose evaluation failed is evaluated again when next referred to;
  • xsl:apply-templates, xsl:call-template, xsl:next-match with xsl:with-param; template, function and global parameters and variables, with as conversion; xsl:function; when each value is computed, as Saxon-HE 12.10 computes it (lazily, inlined, or as its LearningEvaluator learns, see the run module), which decides which dynamic errors are raised;
  • xsl:choose, xsl:if, xsl:for-each, xsl:sequence, xsl:value-of (separator), xsl:attribute, xsl:comment, xsl:text, literal text, xsl:number (level single or multiple, count, format 1), literal result elements with their namespaces;
  • temporary trees (variables whose value is a sequence constructor), simple content and complex content construction as XSLT 2.0 section 5.7 defines them, and sequences (an xsl:function body, a variable or parameter with as): an element, attribute, text node (even of zero length) or comment constructed there is a new parentless node, as the stylesheets compiled by SchXslt construct their reports; copied elements keep their namespaces;
  • document() through a DocumentResolver (the engine reads no file), relative URIs resolved against the base URI given by Executable::with_base_uri, and the source document by its own URI, parsed again as Saxon does; a loaded document has that URI as document URI, the source document none (document-uri(/) is empty, as in Saxon-HE 12.10).

Known differences: Saxon simplifies constant predicates before computing the default priority of a pattern (x[true()] gets the priority of x, 0, where XSLT gives 0.5); this crate uses the pattern as written. The official stylesheets have no constant predicate in a pattern. Error messages are worded differently from Saxon’s (the codes are the same), and some of Saxon’s quote the values involved where this crate’s do not: the warnings for errors in patterns are then fewer distinct messages, for the same patterns and codes. Saxon keeps what its LearningEvaluator learns in the compiled stylesheet, over every document it transforms with it; this crate starts afresh with each transformation, as Saxon does with a stylesheet compiled for one document. The elements of a parsed document have no namespace nodes here: copied to the result, they get only the namespaces their names use.

Limits (TransformOptions): the source document and the documents document() loads are refused when an element is nested deeper than max_depth (einvoice_xpath_eval::DEFAULT_MAX_DEPTH, 256), and a transformation ends when its template rules, built-in rules, named templates and functions are invoked one inside another more than max_nesting deep (DEFAULT_MAX_NESTING) or use more than max_stack bytes of stack (DEFAULT_MAX_STACK), lazy values computed one inside another and the expressions being evaluated included: a LIMIT error (TransformError::limit) instead of a stack overflow, which would abort the process. The stylesheet itself is bounded when it is loaded: its instructions nest einvoice_xslt::MAX_NESTING deep at most, its expressions einvoice_xpath::MAX_DEPTH deep at most, so that loading and preparing it need a bounded stack too (a thread with the default 2 MiB stack, debug builds included). Walking a tree without template rules (copying a node, the path of a node in a warning) uses no recursion. With the defaults, the official stylesheets run on a document 256 deep on a thread with the default 2 MiB stack, debug builds included.

Refused when the stylesheet is prepared, rather than run differently from the reference engine: a parenthesized pattern whose predicate may depend on the position. Everything outside the subset is already refused by einvoice_xslt::load.

let xslt = r#"<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>
  <xsl:template match="/"><total><xsl:value-of select="sum(//l/@a)"/></total></xsl:template>
</xsl:stylesheet>"#;
let executable = einvoice_xslt_exec::Executable::new(&einvoice_xslt::load(xslt)?)?;
let result = executable.transform("<i><l a='1.5'/><l a='2'/></i>", None)?;
assert_eq!(result.output, "<total>3.5</total>");

Structs§

CompactOutput
The serialized result document of Executable::transform_compact, kept as a sequence of pieces, each distinct piece stored once. A piece is what the serializer writes from one tag to the next written in full: an element with its indentation, a start tag, or text with the end tag that follows.
CompactTransformation
The result of Executable::transform_compact.
Executable
A stylesheet ready to run: prepared once, run on any number of documents with Executable::transform.
PrepareError
A problem found while preparing a stylesheet: an XPath expression that does not compile (unknown function or variable, …), a static error of the stylesheet, or a construct outside the supported subset.
PrepareErrors
Every problem found while preparing a stylesheet (at least one).
Serialization
Serialization parameters of the XML output method (xsl:output), as Saxon-HE 12.10 writes XML: UTF-8, namespace declarations before the attributes and sorted by prefix, &lt;, &gt; and &amp; escaped (and quotes, tabs, line ends and carriage returns in attribute values), carriage returns as &#xD;, U+007F to U+009F and U+2028 as character references, three spaces of indentation.
TransformError
A dynamic error that ended a transformation.
TransformOptions
Options of a transformation.
Transformation
The result of a transformation.
TreeTransformation
The result of Executable::transform_tree: the result tree.

Constants§

DEFAULT_MAX_NESTING
The maximum nesting of invocations in a transformation, unless the caller sets another one (TransformOptions::max_nesting): template rules, built-in template rules, named templates and stylesheet functions invoked one inside another. It is max_nesting_for_depth of DEFAULT_MAX_DEPTH: 576.
DEFAULT_MAX_STACK
The default of TransformOptions::max_stack: 1.5 MiB, for a thread with the default 2 MiB stack of a spawned thread; 256 KiB on WebAssembly. With it, the official stylesheets run on a document DEFAULT_MAX_DEPTH deep, in a debug build too, and the 512 KiB left are more than the caller and one level of invocation need.
DEFAULT_MAX_TAIL_CALLS
The most tail calls in a row in a transformation, unless the caller sets another maximum (TransformOptions::max_tail_calls): 65 536. A template that ends with an xsl:call-template runs the template it calls in its place, as Saxon does, without stack in proportion to the recursion; the ISO Schematron skeleton recurses so over each character of every XPath expression of a schema (8519 for the longest of the CEN rules).

Traits§

DocumentResolver
Fetches the documents that document() asks for, by absolute URI. The engine never reads files or the network itself.
ResultObserver
Reads the result document of Executable::transform_compact as it is written, as a parser of its text would read it: the elements with their attributes, the character data (the whitespace the serializer writes to indent included, as text of the element it is in) and the comments, in document order. Nothing outside the document element but comments is reported: the serializer writes no text there but line ends.

Functions§

max_nesting_for_depth
The nesting of invocations that the official stylesheets need on a document max_depth deep, with room to spare: twice the depth, plus 64.
serialize
Writes a tree as XML, as Saxon-HE 12.10 writes it with the XML output method and the parameters settings (Serialization): root is a document node (its children are written) or any other node.