XSLT (Extensible Stylesheet Language Transformations) is a powerful language used to transform XML documents into other formats, such as HTML, text, or even other XML structures. It provides a wide range of elements to perform transformations effectively.
In this comprehensive guide by The Coding College, we’ll cover the most commonly used XSLT elements, their syntax, and their usage.
Core XSLT Elements
1. <xsl:stylesheet>
/ <xsl:transform>
Defines the root element of an XSLT document. Both elements are interchangeable.
Syntax:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- XSLT content -->
</xsl:stylesheet>
2. <xsl:template>
Defines a template rule for matching nodes in the XML document.
Syntax:
<xsl:template match="element_name">
<!-- Transformation logic -->
</xsl:template>
3. <xsl:apply-templates>
Applies the matching templates to the selected nodes.
Syntax:
<xsl:apply-templates select="node"/>
4. <xsl:value-of>
Extracts and outputs the value of a node.
Syntax:
<xsl:value-of select="node"/>
5. <xsl:for-each>
Iterates over a set of nodes and applies a transformation to each one.
Syntax:
<xsl:for-each select="node-set">
<!-- Transformation logic -->
</xsl:for-each>
6. <xsl:if>
Executes content conditionally based on an expression.
Syntax:
<xsl:if test="condition">
<!-- Content if condition is true -->
</xsl:if>
7. <xsl:choose>
Provides multiple conditional branches, similar to an “if-else” structure.
Syntax:
<xsl:choose>
<xsl:when test="condition">
<!-- Content if condition is true -->
</xsl:when>
<xsl:otherwise>
<!-- Content if no conditions are true -->
</xsl:otherwise>
</xsl:choose>
8. <xsl:sort>
Sorts nodes in a specified order, often used within <xsl:for-each>
or <xsl:apply-templates>
.
Syntax:
<xsl:for-each select="node-set">
<xsl:sort select="criteria" order="ascending|descending"/>
<!-- Transformation logic -->
</xsl:for-each>
9. <xsl:output>
Defines the format of the output document (e.g., HTML, XML, or text).
Syntax:
<xsl:output method="xml|html|text" indent="yes|no"/>
Advanced XSLT Elements
10. <xsl:attribute>
Adds an attribute to an element.
Syntax:
<xsl:attribute name="attribute_name">
<xsl:value-of select="value"/>
</xsl:attribute>
11. <xsl:copy>
Copies the current node and its attributes to the output.
Syntax:
<xsl:copy>
<!-- Additional processing -->
</xsl:copy>
12. <xsl:copy-of>
Copies an entire node-set to the output.
Syntax:
<xsl:copy-of select="node-set"/>
13. <xsl:element>
Creates an element dynamically.
Syntax:
<xsl:element name="element_name">
<!-- Content -->
</xsl:element>
14. <xsl:variable>
Defines a variable to store a value.
Syntax:
<xsl:variable name="var_name" select="expression"/>
15. <xsl:param>
Defines a parameter that can be passed to a template.
Syntax:
<xsl:param name="param_name" select="default_value"/>
16. <xsl:key>
Defines a key for use with the key()
function.
Syntax:
<xsl:key name="key_name" match="node" use="value"/>
17. <xsl:call-template>
Calls a named template explicitly.
Syntax:
<xsl:call-template name="template_name"/>
18. <xsl:message>
Outputs a message for debugging purposes.
Syntax:
<xsl:message terminate="yes|no">
<!-- Message content -->
</xsl:message>
19. <xsl:number>
Generates a number for the current node.
Syntax:
<xsl:number level="single|multiple|any"/>
20. <xsl:processing-instruction>
Creates a processing instruction in the output.
Syntax:
<xsl:processing-instruction name="instruction_name">
<!-- Instruction content -->
</xsl:processing-instruction>
21. <xsl:comment>
Adds a comment to the output.
Syntax:
<xsl:comment>
<!-- Comment content -->
</xsl:comment>
22. <xsl:import>
Imports an external XSLT stylesheet.
Syntax:
<xsl:import href="stylesheet_url"/>
23. <xsl:include>
Includes another XSLT stylesheet.
Syntax:
<xsl:include href="stylesheet_url"/>
XSLT Elements Cheat Sheet
Element | Purpose |
---|---|
<xsl:stylesheet> | Defines the XSLT document root |
<xsl:template> | Matches and transforms nodes |
<xsl:apply-templates> | Applies templates to selected nodes |
<xsl:value-of> | Outputs the value of a node |
<xsl:if> | Executes content conditionally |
<xsl:choose> | Implements “if-else” logic |
<xsl:for-each> | Loops through node-sets |
<xsl:sort> | Sorts nodes in a specified order |
<xsl:copy> | Copies the current node |
<xsl:output> | Defines the output format |
<xsl:element> | Dynamically creates an element |
<xsl:attribute> | Adds attributes to elements |
<xsl:variable> | Declares variables for reuse |
<xsl:param> | Declares parameters for templates |
<xsl:call-template> | Calls a named template |
<xsl:comment> | Adds comments to the output |
<xsl:message> | Outputs messages for debugging |
Conclusion
XSLT provides a rich set of elements to handle complex XML transformations. By mastering these elements, you can build powerful transformations and generate flexible, reusable stylesheets for your XML data.
For more XSLT tutorials and examples, visit The Coding College, where we provide in-depth resources to help you master coding and programming concepts.