XSLT is a language that transforms XML documents into your required format like into other XML documents, HTML format, CSV file format…etc. In this article we discuss about what XSLT is and how it converts XML document into other formats.
XSLT is also a XML document that has specialized XML elements and special instructions to convert XML document into other format. XSLT is a style sheet that converts XML document into other format depending on the instructions it has.
In this article we discuss about how to transform XML file into HTML format by using XSLT style sheet. For example you have XML file which has information about a lot of books available in a library. If you want to make this information available in online for your users, you can make it easily by using XSLT style sheet instead of using any other programming language.
Here you have a XML file which contains available book titles as shown below.
<?xmlversion="1.0"?>
<Books>
<Book>
<title>Apress.Beginning.CSharp.Object-Oriented.Programming.May.2011</title>
</Book>
<Book>
<title>OReilly.C.Sharp.3.0.Design.Patterns.Jan.2008</title>
</Book>
<Book>
<title>Apress Professional InfoPath 2007</title>
</Book>
<Book>
<title>Apress.Pro.SQL.Server.2008.Reporting.Services</title>
</Book>
<Book>
<title>Apress.Pro.WCF.Practical.Microsoft.SOA.Implementation</title>
</Book>
</Books>
We created the XSLT style sheet to display the above XML file content as shown below.
<?xmlversion="1.0"?>
<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:outputmethod="html" />
<xsl:templatematch="/">
<html>
<title>Books Available in Library</title>
<body>
<h2>Dotnet Books Available</h2>
<tableborder="1">
<trstyle="background:#CCC;Color:#FFF;">
<thalign="center">Book Title</th>
</tr>
<xsl:for-eachselect="Books/Book">
<tr>
<td>
<xsl:value-ofselect="title"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you observe the above XSLT file, it has HTML content <head>,<title> and <body> tags. In <body> tag we are looping the XML file elements by using xsl for-each statement. Here we are looping Books.xml file content by providing the root element and each individual element for for-each statement(Books/Book). And we are displaying the book title by using xsl value-of statement. For every XSL statement we have to mention the XML element name for which element statement has to execute.
Now we have to register this XSLT file in our XML file by using xml-stylesheet element as shown below.
<?xmlversion="1.0"?>
<?xml-stylesheettype="text/xsl" href="Books.xslt"?>
<Books>
<Book>
<title>Apress.Beginning.CSharp.Object-Oriented.Programming.May.2011</title>
</Book>
<Book>
<title>OReilly.C.Sharp.3.0.Design.Patterns.Jan.2008</title>
</Book>
<Book>
<title>Apress Professional InfoPath 2007</title>
</Book>
<Book>
<title>Apress.Pro.SQL.Server.2008.Reporting.Services</title>
</Book>
<Book>
<title>Apress.Pro.WCF.Practical.Microsoft.SOA.Implementation</title>
</Book>
</Books>
If you open this XML file in browser, the output shown as below.