|« Upgrading Plesk 8.3.0 to 8.4.0 to 8.6.0||B2evolution - FreePHP Widget Plugin »|
Simple XSL / XSLT ExampleSeptember 9th, 2008
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.
Now we need a way of transforming the XML data into HTML which can be understood and drawn by a browser.
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.
Next is the root object called stylesheet. To conform to standards we must supply the name spaces that the stylesheet document uses.
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.
Load jobs.xml into your browser and marvel!