XSLT
Transform XML documents with XSLT β the declarative transformation language
#XSLT
XSLT (Extensible Stylesheet Language Transformations) is a declarative language for transforming XML documents. It matches patterns on the XML tree to produce any output format.
XSLT transforms XML data into HTML pages, other XML formats, JSON, CSV, or plain text. It is the transformation layer that makes XML practical.
#Getting Started
-
Your First Transform β A hands-on introduction. Transform XML to HTML, step by step.
-
Template Matching β The core paradigm shift: declarative rules instead of imperative code.
-
Output Methods β Producing HTML, XML, JSON, CSV, and text from the same source data.
-
Extensibility β Custom functions, packages, extension instructions, and .NET integration.
#Instructions Reference
The complete set of XSLT 3.0/4.0 instructions, organized by purpose:
-
Instructions Overview β Index of all instruction categories.
-
Output β value-of, text, sequence, message.
-
Control Flow β if, choose, switch (4.0), where-populated.
-
Variables and Parameters β variable, param, static params, tunnel params.
-
Iteration and Sorting β for-each, sort, iterate/break/next-iteration.
-
Grouping β for-each-group with all four modes.
-
Copying and Identity β copy, copy-of, identity transform pattern.
-
Node Construction β element, attribute, namespace, AVTs.
-
Multiple Outputs β result-document, named output formats.
-
Functions β Defining reusable XPath functions.
-
Keys β Indexing for fast lookup.
-
String Analysis β Regex-based processing.
-
Error Handling β try/catch, assert, fallback.
-
Merging β Combining sorted inputs.
-
Maps, Arrays, Records β Structured data (3.0/4.0).
-
Streaming β Large document processing with accumulators.
-
Packages β Reusable stylesheet libraries.
-
Dynamic Evaluation β Runtime XPath evaluation.
-
Number Formatting β Numbering, decimal formats, character maps.
#Streaming
PhoenixmlDb supports XSLT 3.0 streaming for processing documents that are too large to fit in memory. Streaming processes the input as a forward-only event stream, never building the full tree.
-
Streaming and Accumulators β
xsl:mode streamable="yes",xsl:source-document, accumulators,xsl:fork, and streamability rules.
Key capabilities:
-
Primary source streaming β Declare
xsl:mode streamable="yes"to stream the principal input document via XmlReader. -
External document streaming β Use
xsl:source-document streamable="yes"to stream documents loaded during transformation. -
Accumulators β Maintain running state (counts, totals, max values) as nodes flow past.
-
Static streamability checking β The compiler analyzes your stylesheet and reports streamability violations at compile time.
-
system-property('xsl:supports-streaming')returns"yes". -
Limitations:
last()is not available in streaming mode.xsl:forkbranches execute sequentially, not in parallel.
#The Mental Model
XSLT uses declarative rules instead of imperative code. A rule states what output to produce for a given pattern. The engine handles the traversal.
|
C# Concept |
XSLT Equivalent |
|---|---|
|
A method that takes input and returns output |
A template rule that matches a node and produces output |
|
|
Template matching with |
|
|
|
|
String interpolation |
Attribute value templates: |
|
Method overloading |
Template priority and import precedence |
For C# developers: several XSLT constructs mirror familiar C# patterns. Pattern matching on XML trees resembles pattern matching in a Razor view or a CSS selector. The engine runs the traversal automatically. A template rule resembles a method that takes input and returns output. Template matching resembles a
switchon type.xsl:for-eachandxsl:apply-templatesresembleforeach. Attribute value templates resemble string interpolation. Template priority and import precedence resemble method overloading.