All you should know about XSLT Transformation

What is XSLT?

XSLT, Extensible Stylesheet Language Transformations provides the ability to transform XML data from one format to another automatically.

How XSLT works?

An XSLT stylesheet is used to define the transformation rules to be applied on the target XML document. XSLT stylesheet is written in XML format.

Why do we need this?

We need XSLT for any kind of transformation on xml and do complex calculations, sorting, grouping, sum etc. And that too on a separate XSL file. Here is the example where we are converting xml doc to html using xslt.

XML Code:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
<student>Jack</student>
<student>Harry</student>
<student>Rebecca</student>
<teacher>Mr. Bean</teacher>
</class>

XSLT Code:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="teacher">
<p><u><xsl:value-of select="."/></u></p>
</xsl:template>

<xsl:template match="student">
<p><b><xsl:value-of select="."/></b></p>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

XSLT Output (What you see in your browser):

<html>
<body>
<p><b>Jack</b></p>
<p><b>Harry</b></p>
<p><b>Rebecca</b></p>
<p><u>Mr. Bean</u></p>
</body>
</html>

XSLT Syntax and its Uses:

<xsl:template>  defines a way to reuse templates in order to generate the desired output for nodes of a particular type/context.

Following is the syntax declaration of <xsl:template> element

<xsl:template
name= Qname
match = Pattern
priority = number
mode = QName
</xsl:template>

 

<xsl:value-of> tag puts the value of the selected node as per XPath expression, as text.

Following is the syntax declaration of <xsl:value-of> element

<xsl:value-of
select = Expression
disable-output-escaping = "yes" | "no"
</xsl:value-of>

 

<xsl:for-each> tag applies a template repeatedly for each node.

Following is the syntax declaration of <xsl:for-each> element

<xsl:for-each
select = Expression
</xsl:for-each>

 

<xsl:sort> tag specifies a sort criteria on the nodes

Following is the syntax declaration of <xsl:sort> element

<xsl:sort
select = string-expression
lang = { nmtoken }
data-type = { "text" | "number" | QName }
order = { "ascending" | "descending" }
case-order = { "upper-first" | "lower-first" }
</xsl:sort>

 

<xsl:if> tag specifies a conditional test against on the content of nodes

Following is the syntax declaration of <xsl:if> element

<xsl:if
test = boolean-expression
</xsl:if>

 

<xsl:choose> tag specifies a muliple conditional tests against on the content of nodes in conjunction with the <xsl:otherwise> and <xsl:when> elements.

Following is the syntax declaration of <xsl:choose> element

<xsl:choose>
<xsl:when test="marks > 90">
High
</xsl:when>
<xsl:when test="marks > 85">
Medium
</xsl:when>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>

 

<xsl:key> tag element specifies a named name-value pair assigned to a specific element in an XML document. This key is used with the key() function in XPath expressions to access the assigned elements in a XML document.

Following is the syntax declaration of <xsl:key> element

<xsl:key
name = QName
match = Pattern
use = Expression
</xsl:key>

 

<xsl:apply-template> tag signals the XSLT processor to find the appropriate template to apply, based on the type and context of each selected node.
Following is the syntax declaration of <xsl:apply-template> element

<xsl:apply-template
select = Expression
mode = QName
</xsl:apply-template>

Grouping in XSLT

Below example is to group files by their projects.

XML example to sort and group by

<files>
<file name="swablr.eps" size="4313" project="mars"/>
<file name="batboy.wks" size="424" project="neptune"/>
<file name="potrzebie.dbf" size="1102" project="jupiter"/>
<file name="kwatz.xom" size="43" project="jupiter"/>
<file name="paisley.doc" size="988" project="neptune"/>
<file name="ummagumma.zip" size="2441" project="mars"/>
<file name="schtroumpf.txt" size="389" project="mars"/>
<file name="mondegreen.doc" size="1993" project="neptune"/>
<file name="gadabout.pas" size="685" project="jupiter"/>
</files>

XSLT Example

<xsl:template match="files">

<xsl:for-each-group select="file" group-by="@project">
<!-- group by project attribute -->
<!-- we can also use group-by, group-adjacent, group-starting-with, and group-ending-with -->
<xsl:for-each select="current-group()">
<!-- loop for each group -->
<xsl:value-of select="@name"/>, <xsl:value-of select="@size"/>
<!-- write value for name and size -->
<xsl:text>
</xsl:text>
</xsl:for-each>

<xsl:text>average size for </xsl:text>
<xsl:value-of select="current-grouping-key()"/>
<xsl:text> group: </xsl:text>
<xsl:value-of select="avg(current-group()/@size)"/>
<xsl:text>

</xsl:text>
</xsl:for-each-group>

</xsl:template>

XSLT Output

swablr.eps, 4313
ummagumma.zip, 2441
schtroumpf.txt, 389
average size for mars group: 2381

batboy.wks, 424
paisley.doc, 988
mondegreen.doc, 1993
average size for neptune group: 1135

potrzebie.dbf, 1102
kwatz.xom, 43
gadabout.pas, 685
average size for jupiter group: 610

 

[su_divider style=”dotted”]

@skmeSiebel

Leave a Reply

Your email address will not be published. Required fields are marked *