XSLT Basics: Transforming XML Data
XSLT is a transformation language that converts XML documents into other formats like HTML, plain text, or restructured XML. This post covers the three core components of an XSLT transformation, walks through a practical device inventory example, and introduces key XSLT elements like templates, val
What Is XSLT and Why Should You Care?
If you have ever worked with XML data and wished you could reshape it into something more useful, XSLT is the tool for the job. XSLT stands for Extensible Stylesheet Language Transformations. It is a language designed specifically for transforming XML documents into other formats, whether that is a different XML structure, an HTML page, plain text, or even a CSV file.
Think of XSLT like a blueprint. You feed it an XML document and a set of rules (the stylesheet), and it produces a brand new output. This is especially useful in networking and automation workflows where APIs return XML data that needs to be reformatted before it is useful to another system.
The Three Players in an XSLT Transformation
Every XSLT transformation involves three things working together:
- The source XML document: the raw data you want to transform
- The XSLT stylesheet: the set of rules that define how to transform it
- The XSLT processor: the engine that reads both and produces the output
Popular XSLT processors include xsltproc on Linux, Saxon, and Xalan. Most modern programming languages also have built-in or library-based XSLT support.
A Simple Example: Transforming a Device Inventory
Let us start with a simple XML file representing a network device inventory.
<?xml version="1.0" encoding="UTF-8"?>
<inventory>
<device>
<hostname>router-01</hostname>
<ip>192.168.1.1</ip>
<type>router</type>
</device>
<device>
<hostname>switch-01</hostname>
<ip>192.168.1.2</ip>
<type>switch</type>
</device>
</inventory>Now here is an XSLT stylesheet that transforms this into a simple HTML table.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Device Inventory</h2>
<table border="1">
<tr>
<th>Hostname</th>
<th>IP Address</th>
<th>Type</th>
</tr>
<xsl:for-each select="inventory/device">
<tr>
<td><xsl:value-of select="hostname"/></td>
<td><xsl:value-of select="ip"/></td>
<td><xsl:value-of select="type"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>To run this on Linux using xsltproc, save the XML as inventory.xml and the stylesheet as transform.xsl, then run:
xsltproc transform.xsl inventory.xmlThe output will be a valid HTML table built directly from your XML data.
Key XSLT Concepts to Know
Templates
The core building block of any XSLT stylesheet is the <xsl:template> element. The match attribute uses XPath expressions to target specific nodes in your XML. Using match="/" targets the root of the document, which is usually where you start.
Value-of
The <xsl:value-of select="..."/> element extracts the text content of a node. The select attribute takes an XPath expression pointing to the element you want.
For-each
When you need to loop through repeated elements, <xsl:for-each select="..."/> iterates over every matching node, similar to a for loop in Python.
If and Choose
XSLT also supports conditional logic. Use <xsl:if test="..."> for simple conditions, or <xsl:choose> with <xsl:when> blocks for more complex branching, similar to an if/else structure.
Where XSLT Fits in the Real World
XSLT is used heavily in enterprise integration, where systems exchange XML-formatted data and each system expects a slightly different structure. In networking, tools like NETCONF return XML responses from network devices. XSLT gives you a clean, declarative way to extract and reformat that data without writing verbose parsing code.
It is also worth noting that XSLT relies on XPath for navigating XML trees. If XPath is unfamiliar to you, spending time there will make XSLT click much faster.
What's Next
Now that you understand how XSLT transforms XML data, the next step is diving deeper into XPath expressions. XPath is the navigation language that powers the select and match attributes in your stylesheets. Understanding XPath properly will give you precise control over which nodes you target and transform. We will cover XPath syntax and practical examples in the next post.
Free Resources to Go Deeper
- W3Schools XSLT Tutorial: w3schools.com/xml/xsl_intro.asp - a solid interactive reference for beginners
- MDN XML Introduction: developer.mozilla.org - covers the XML foundation underneath XSLT
- freeCodeCamp XML and XSLT content: freecodecamp.org - practical exercises to reinforce the concepts