--- title: "XSLT for beginners: your first transformation" description: "Learn XSLT from scratch with practical examples. Transform XML step by step using templates, match patterns, and value-of." date: 2025-02-20T00:00:00Z tags: ["beginners", "xslt", "tutorial"] --- XSLT is a language for transforming XML documents into other formats — another XML structure, HTML, plain text, or JSON. If you are new to it, the learning curve can feel steep because XSLT is declarative and template-driven, which is different from procedural languages. This guide walks you through the core ideas with working examples you can run in [XSLT Playground](https://xsltplayground.com) right now. ## What XSLT does You start with an XML source document. You write a stylesheet that describes rules for transforming it. The processor reads both and produces an output document. The stylesheet does not loop through the input line by line — instead, it defines templates that match nodes, and the processor calls those templates as it traverses the document tree. ## Your first stylesheet Start with a simple XML document: ```xml Design Patterns Gang of Four 45.00 Clean Code Robert Martin 38.00 ``` Now write a stylesheet that turns this into an HTML table: ```xml
Title Author Price
``` Paste both into [XSLT Playground](https://xsltplayground.com) and run it. You will see an HTML table with the book data. ## Understanding templates The `` rule fires when the processor reaches the document root. Inside it, `` tells the processor to find all `book` elements inside `catalog` and call the matching template for each one. The second template `` fires once per `book` element. Inside it, `` extracts the text content of the `title` child. This is the core loop of XSLT: match, apply, select. ## XPath selects nodes The `select` and `match` attributes use XPath, a path language for navigating XML trees. A few rules you will use constantly: | XPath | Meaning | |---|---| | `catalog/book` | `book` children of `catalog` | | `//book` | All `book` elements anywhere in the document | | `book/@id` | The `id` attribute of `book` | | `book[price > 40]` | Books whose price exceeds 40 | | `.` | The current node | | `..` | The parent of the current node | ## Filtering with predicates Add a predicate to the `apply-templates` call to show only books over 40: ```xml ``` Or use `xsl:if` inside the template: ```xml ``` ## Sorting output Use `xsl:sort` to control the order: ```xml ``` ## What to try next Once you are comfortable with templates and XPath: - Learn `xsl:for-each` for inline iteration - Explore `xsl:choose` for multi-branch conditionals - Try `xsl:variable` to store intermediate values - Look at `xsl:param` to pass values into your stylesheet from outside All of these work in XSLT 1.0, 2.0, and 3.0. The [XSLT Playground](https://xsltplayground.com) lets you set the version and experiment without installing anything.