Bookmark and Share

XSLT - Introduction

XSLT is an XML-related technology that is used to manipulate and transform XML documents. The acronym XSLT stands for Extensible Stylesheet Language Transformations, which is a mouthful to say, but it sounds more complicated than it is.

Advertise on Tizag.com

With XSLT, you can take an XML document and choose the elements and values you want, then generate a new file with your choices. Because of XSLT's ability to change the content of an XML document, XSLT is referred to as the stylesheet for XML.

If you have already heard about HTML and CSS, you know that CSS is a way of styling HTML. In a similar relationship, XSLT is used to style and transform XML.

XSLT - Know Your XPath!

It is virtually impossible to do anything in XSLT without using its sister technology, XPath. Before you can begin XSLT, you need to understand the basics of XPath, so if you haven't already, go through our XPath Tutorial.

XSLT - Quick Example

To give you an idea of what you'll be learning with XSLT, we've provided a simple XML document that gets transformed by a complicated looking XSLT file.

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>

The XML file class.xml is linked to the XSLT code by adding the xml-stylesheet reference. The XSLT code then applies its rules to transform the XML document.

However, if you were to "view the source" of these XML files in your browser, you would just see the XML document and not the transformed file. XSLT does not change an XML document, but this example shows how XSLT can be used to temporarily manipulate XML.

Below, we have manually reconstructed the XSLT output that you see when you click class.xml. This HTML was created from an XML document bring transformed by our XSLT code.

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>

As you can see, we used XSLT to convert the XML document into a simple webpage. Because we are an internet-related website, all of our XSLT lessons will be focusing on browser ready XSLT output. However, you can pretty much do anything with XSLT!

XSLT - Time to Learn

Now that you've seen one of the applications of XSLT, it's time to get into the nitty-gritty details. The next few lessons will teach you the basics of XSLT, so that you can begin to write your own XML transformations with XSLT!

Bookmark and Share




Download Tizag.com's XML Book

If you would rather download the PDF of this tutorial, check out our XML eBook from the Tizag.com store.

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials