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.
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.
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.
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 version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class.xsl"?> <class> <student> Jack </student> <student> Harry </student> <student> Rebecca </student> <student> Mr. Bean </student>
<?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"> <xsl:template match="/"><html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> </xsl:styleshee>
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.
Before XSLT: classoriginal.xml
After XSLT rules are applied: class.xml
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.
<html> <class> <p><b> Jack </b></p> <p><b> Harry </b></p> <p><b> Rebecca </b></p> <p><ul> Mr. Bean </ul></p> </html> </body>
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!
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!