Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m Advertisement # Getting Started With XSL(T) Difficulty:IntermediateLength:LongLanguages: In this tutorial, we will adventure into the world of XSL(T) and explain what it is, how to pull data from an XML document, basic iteration and basic login and conditional statements. ### Tutorial Details • Program: Any Text Editor • Version: 1 of 1 • Difficulty: Medium • Estimated Completion Time: 25min ### Overview Sometimes, you have huge datasets that are parsed out as XML that need to be formatted so that someone who doesn't know how to read XML can read the data. In this tutorial, I will show you how that is done with the power of XSL(T). ### What is XSL(T)? XSL(T) is short for Extendable Stylesheet Language (Transformation). Even though it is a "stylesheet," it has a different goal than CSS (Cascading Stylesheets). XSL(T) is not used for visual effect, but instead extracting data (or transforming) from XML and using the combination of HTML and CSS to format them. XSL(T) also has the dynamic properties in the sense that you can do iteration and conditional statements upon a static XML file. XSL(T) is not used for visual effect, but instead extracting data (or transforming) from XML and using the combination of HTML and CSS to format them. ### Why use XSL(T)? XSL(T) can be used for organizing large trees of XML so that anyone can read it. For instance a Google Search Appliance serves up queries as XML. For that XML to be read by a general user some transformation has to occur. This is where XSL(T) plays a vital role. It could also be used to style an RSS feed page since the source is all in XML. It is also used as the main templating language for Autonomy's Teamsite (EMS) and such open source CMSs as Symphony. XSL(T) also cuts down on server load. Since XSLT can do the transformation on the client side, your server has to do less work, since you are not querying upon data in database. JavaScript and server-side functions can be setup to tell the document to use a specific XSL(T) template or you can source the template within the actual XML file. For this tutorial, we are simply going to source the XSL(T) template in the XML file. ### Getting Started Our goal is to create a listing of vacations that we want to go on, and perform some logic on the data to let us know what destinations are out of our price range. In this case our budget is$999 for our vacation and we are going to indicate when the price is over our budget. We will also be ordering them in descending order so we can see right away what trips are out of our budget.

First off you are going to need an XML file. Go ahead and open your favorite text editor and create a new file and call it trips.xml. I have provided a sample XML file you can download and use or you can copy and paste in the code below.

The first and only thing we will need to do to this document is to add a reference to where our .xsl file lives. In this instance we will be creating a stylesheet called trips.xsl.

### Beginning The Extendable Stylesheet

Start by creating a new document in your favorite text editor and saving it as trips.xsl. Next we can start the actual stylesheet .You will first need to specify the XML version and encoding for the template. If you are familiar with XML, it is the same version and encoding syntax

From here we can dive into writing some XSL. To start the stylesheet we must first let the browser know that we are using an XSL stylesheet and what version. This element must wrap all of your mark up and should be closed out at the end of the document otherwise the document will not transform.

If we are going to want to validate the XHTML within our document against W3C standards we then will need to include a doctype to use upon transformation and render. Here we are going to use the XHTML Strict DTD. We do this by setting up a self closing element called xsl:output. With in that element we will call the doctype.

Next, we will start the actual template. In between the template open and close tag is where all of out XHTML will go along any other XSL(T) elements. It is important to note that you have set a match for the template. This attribute basically says go to the root of the XML document.

### Transformation

First thing we need to do now is start up a basic XHTML layout and nest that within our xsl:template tags. I also have linked a css file to document to give the document some style but setting that up is beyond the scope of this tutorial.

So our first goal is to take the title element from the XML document and place that data into the title of the html document.

Before we we start with the XSL it is important to note where within the XML tree "title" exist. In this case title is a direct descendant of the root element of vacation. We can now start to writing our XSL. To be able to tell the browser where a matched piece of data is going to live we use the xsl:value-of element.

We are in the root of document already through the xsl:template tag but we must dig deeper into the XML tree to select the data we want. In this case we are going to look in the document for a root element of vacation with child of title.

When we go to open our XML file (trips.xml) in the browser our result is the title element being displayed in title bar of the browser, where we would expect it to be in a normal XHMTL based site. Also notice we no longer see the XML tree structure, however when we view source we see that the source of the document is XML.

We can take it one step further to prove that the transformation from XML to XHTML has occurred in the browser by using a tool like FireBug or Safari Inspector.

To add some visual organization I am going to add some markup so that our data is a little bit more easily to look at.

As you can see above, I have reused the title element once again but this time in the body of the document. This can be extremely useful in the re-use and repurposing of content.

Next, we are going to want to transform our "trips" into XHTML. Common sense says that we should just be able to call another xsl:value-of but this time calling upon the child node of "trip". This would work if we only had one trip. However since we have multiple trips we need to do some iteration or looping through the XML document.

### Iteration aka Looping

As in all programming languages there is iteration statements that loop through a specific dataset and performs some methods on them. XLS(T) has similar functionality in extracting data from an XML document.

The snippet above does pretty much what you would expect it to do. It is saying xsl:for-each element select all that exists at the path vacation/tip.

We next have to state what we want to happen to the other items if they meet our constraint of under $999. In this case we are just going to print them out in a list item with no special class attached to them. We are going through and selecting all of the same nodes as we did for the too-much nodes (price, city,state,country and date). ### Putting It All Togeather By this time we should actually have something useful to look at in the browser. Open up trips.xml like before. If all has gone well you should have something similar to what is shown above. XSL(T) is a very powerful language that allows you to drastically change how XML data is presented. Below are some resources to learn more about XSL(T). ### Write a Plus Tutorial Did you know that you can earn up to$600 for writing a PLUS tutorial and/or screencast for us? We're looking for in depth and well-written tutorials on HTML, CSS, PHP, and JavaScript. If you're of the ability, please contact Jeffrey at nettuts@tutsplus.com.

Please note that actual compensation will be dependent upon the quality of the final tutorial and screencast.