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:

  1. Stylesheet Processing: The XSLT document is read and compiled into an internal executable structure or bytecode.
  2. Source Document Parsing: The source XML is parsed into an optimized in-memory tree representation.
  3. Template Matching and Execution: The transformation engine processes the source tree starting from the root node, resolving XPath queries and executing matching XSLT instructions.
  4. 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:

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.”

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.