Simple XSL / XSLT Example
Posted by davidnewcomb on 09 Sep 2008 in Techie
XSLT stands for eXtensible Stylesheet Language Transformation. It is a language for translating XML documents into other XML documents. “What’s the point of that?", I hear you ask. Almost all computer systems need to exchange information. XML is a standardise way of achieving this. It provides a way of describing data. Once the data is in this standardised form it can be translated into any format by a receiving system. The receiving system can turn it into it’s own native form for handling within its own domain.
Application designers want the minimum fuss within their applications, they don’t want to spent all their time converting objects from one format to another to make the data work in different ways for them. Many modules within an application communicate via XML. One of the main applications of this is displaying the data on web pages. A good example of this is a search engine.
Your application may provide search capabilities via a servlet. A client may provide some search criteria and receive back a set of results in XML format. It is then up to the client application to display the results. It is not the responsibility of the search engine to dictate how the results are displayed. The results displayer can be modified to display the results in a different way if it chooses. It may decide to show some of the information in a different way or not to display it at all. We now have a separation of concerns, which can be handled independently by 2 different parties.
This is where XSLT comes in. XSLT provides a way of turning the XML document into a language that a database understands or something that can be displayed in a web browser. The underlying XML search results do not need to change - only the transformation of them. An example is the best way to show this in action.
In this example, our search provides a list of name jokes (I love them!). We get back an XML document containing the search results. The results are just the data - there is no formatting.
jokes.xml
:
<?xml version="1.0" ?>
<jokes>
<joke id="123">
<question>What do you call a man with a spade on his head</question>
<answer>Edward</answer>
</joke>
<joke id="456">
<question>What do you call a man with a seagull on his head</question>
<answer>Cliff</answer>
</joke>
</jokes>
Now we need a way of transforming the XML data into HTML which can be understood and drawn by a browser.
jokes.xsl
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html"/>
<xsl:template match="joke">
<p>
<b><xsl:value-of select="question"/>?</b><br/>
<xsl:value-of select="answer"/>
</p>
</xsl:template>
<html>
<head>
<title>Name jokes results</title>
</head>
<body>
<xsl:template match="/jokes">
<xsl:apply-templates select="joke"/>
</xsl:template>
</body>
</html>
</xsl:stylesheet>
The document is split into several sections, beginning with the standard XML header which tells us the type of document that this is. In this case it is a standard XML document that uses UTF-8 character encoding.
<?xml version="1.0" encoding="UTF-8"?>
Next is the root object called stylesheet. To conform to standards we must supply the name spaces that the stylesheet document uses.
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
All tags starting with xmlns:xsl are defined in http://www.w3.org/1999/XSL/Transform, all other tags without a name: are defined in http://www.w3.org/1999/xhtml and are, in our case, standard HTML mark-up tags.
The final stage is to associate the XML document with the XSL document. Now the application designer can concentrate on providing the raw data and out-source the front end look and feel to a third party.
jokes.xml
:
<?xml version="1.0" ?>
<?xml-stylesheet href="jokes.xsl" type="text/xsl" ?>
<jokes>
<joke>
<question>What do you call a man with a seagull on his head</question>
<answer>Cliff</answer>
</joke>
<joke>
<question>What do you call a man with a spade on his head</question>
<answer>Edward</answer>
</joke>
</jokes>
Load jobs.xml into your browser and marvel!
Job done!1 comment
Thanks, that was a handy introduction.
Form is loading...