How Apache Xalan Executes XSLT Stylesheets
Apache Xalan is a robust processor designed to transform XML documents into HTML, plain text, or other XML formats using Extensible Stylesheet Language Transformations (XSLT). This article explains the internal execution pipeline of Apache Xalan, detailing how it parses source files, organizes nodes in memory using the Document Table Model (DTM), evaluates XPath expressions, and applies template rules through both interpretive and compiled (XSLTC) execution modes.
1. The Processing Pipeline Overview
Xalan follows the standard Java API for XML Processing (JAXP) model to execute transformations. The execution flow consists of four primary stages:
- Stylesheet Processing: The XSLT document is read and compiled into an internal executable structure or bytecode.
- Source Document Parsing: The source XML is parsed into an optimized in-memory tree representation.
- Template Matching and Execution: The transformation engine processes the source tree starting from the root node, resolving XPath queries and executing matching XSLT instructions.
- Result Serialization: The generated result tree is serialized to the requested output format (XML, HTML, or text) via SAX events, DOM nodes, or direct character streams.
2. Document Table Model (DTM)
Standard DOM implementations introduce significant memory overhead because every element, attribute, and text node is stored as a separate Java object. To optimize memory usage and traversal speed, Xalan uses the Document Table Model (DTM).
The DTM represents the entire XML document tree using parallel arrays of primitive integers rather than complex object graphs. Each node is assigned an integer handle that encodes its node type, document identity, and array offset. Structural relationships (parent, first child, next sibling) are resolved through direct array index lookups. This flat architecture drastically reduces garbage collection pressure and accelerates tree traversals.
3. XPath Evaluation and Pattern Matching
At the core of stylesheet execution is pattern matching driven by Xalan’s XPath engine:
- Expression Parsing: XPath expressions in
matchandselectattributes are parsed into Abstract Syntax Trees (ASTs). - Context Handling: As Xalan traverses the XML tree, it maintains an execution context containing the current context node, context position, and variable bindings.
- Template Resolution: When a node is selected for
processing (such as via
<xsl:apply-templates>), Xalan evaluates all matching<xsl:template>rules, computes their specificity and priority, and executes the rule with the highest precedence.
4. Interpretive Mode vs. Compiled Mode (XSLTC)
Apache Xalan provides two distinct processing engines to execute stylesheets:
The Interpretive Processor
In interpretive mode (org.apache.xalan.processor and
org.apache.xalan.transformer), Xalan converts the
stylesheet into a runtime graph of ElemTemplateElement
objects. During transformation, an interpreter walks through these
objects sequentially, dynamically evaluating variables, conditions
(<xsl:if>, <xsl:choose>), and
loops (<xsl:for-each>). This mode requires minimal
initial startup time and is suitable for dynamic environments where
stylesheets change frequently.
The Compiled Processor (XSLTC)
XSLTC compiles an XSLT stylesheet directly into Java bytecode, producing classes known as “translets.”
- Ahead-of-Time / On-the-Fly Compilation: The compiler translates XSLT elements and XPath expressions directly into JVM instructions.
- Performance Optimization: Translets eliminate the interpretation overhead, inline variable lookups, and execute loops directly as compiled JVM code.
- Reuse: Once compiled into a
Templatesobject, a translet can be instantiated across multiple threads as lightweightTransformerinstances to transform large volumes of XML documents with high throughput.
5. Serialization
Once the transformation logic processes the nodes, the result tree is
piped through a serializer. Xalan dispatches the generated output as a
stream of SAX-like events (startElement,
characters, endElement) to an output handler.
The serializer applies output properties specified in
<xsl:output> (such as indentation, character
encoding, and media types) and writes the final document to a file,
stream, or memory buffer.