XSLT

Definition

XSLT

XSL = eXtensible Stylesheet Language for XML (like CSS for HTML)

XSLT = XSL Transformations / templates

XML documents that match the input document and define the output:

XML document\mapstoXML/HTML/txt document

XSLT Templates

Template

<xsl:templatematch="pattern">

match element nodes, replace with content

does not apply pattern to children of the node - must be done explicitly:

<xsl:apply-templatesselect="xPath expression">

Default template

When template matches element without any instructions, then the default template (which is always present) gets applied:

elements apply childrens template to parents

text copy content to the output

attributes copy value to the output

Priorisation of templates

Only 1 template is applied based on its priority value (defined by the specificity of XPath expression)

If ambiguious (two templates get matched for the same element) then this causes an error.

Named templates

<xsl:call-templatename="...">

used to avoid writing the same template for multiple elements

<xsl:template match="name | email">
<xsl:call-template name="output"/>
</xsl:template><xsl:template name="output">
Below you'll see either a name or mail:
<xsl:value-of select="."/>
</xsl:template>

Creating output

<xsl:element name="..."> create an element

<xsl:attribute name="..."> create an attribute

<xsl:text name="..."> create a text

<xsl:comment name="..."> add a comment

Other Features

<xsl:for-each select="..."> select and iterate

<xsl:copy-of select="..."> creates a deep-copy of the selected node

<xsl:sort select=".." order=".."> sorts ascending / descending (used in for-each loop)

<xsl:if test="..."> applies content if test is evaluated to true

<xsl:choose select="..."> uses <xsl:when test="..."> and <xsl:otherwise>

<xsl:variable select="..."> used to bypass restrictions with complex XPath expressions