Thursday, 20 September 2012

Newbies Guide To Employing XSLT

XSLT is short for extensible stylesheet language transformations, and it is an highly flexible and handy technique that claims numerous benefits in particular when applied to tackle the proper troubles.

So that you can get you started, all you need to have is the appropriate overview to the basic principles of XSLT to assist you get started out, and once you do, you surely won’t look back, it will definitely turn into one of the most invaluable tools in your programming tool bet.

And so why might you use XSLT and what can it achieve? In really basic terms, XSLT is a transformation dependent formatter which makes it possible for you to change structured XML documents straight into different output formats, for example into HTML or CSV and so on.

For the purposes of this short article we are going to take a look at the capacity of XSLT to transform or alter XML records into HTML. We are going to employ an XML file to illustrate the attributes of a product (e.g. the Name, Short Description, Long Description, Downloads, Licensing Options, System Requirements, etc.) and also a series of XSLT files to display them.

Right, firstly let’s put together our own very straightforward XML document for a product, and you can develop it up from there. For the time being, all we will cover is the product title, version, and short description. You are able to create more later on should you feel brave enough.

<product code="Spt001">

 <title>htmltrans</title>

 <version>2.0</version>

 <short_desc>

 The product spt001 enables transformation of regular text to html and vice versa

 </short_desc>

</product>

So that’s our basic XML document all set up, then were ready to operate with XSLT and carry out some basic functions

Now that we have got the basic XML document set up, let me look at what we could do using XSLT.

XSLT, like virtually all other XML-based formats, is pretty fastidious about its structure. Your simple XSLT document appears just like this:

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

 <xsl:output method="html" />

 <xsl:template match="/">

 Rules go in here...

 </xsl:template>

</xsl:stylesheet>

Well the above illustration is a pretty simple and common xslt file, anyone can see we have declared the XSLT stylesheet and version. In the fourth line we have also told the parser to output the result as HTML.

Observe we also have a xsl:template component declared in the 5th row.

Bear in mind that XSLT is a rules-based language, so in compliance with the principles, the parser will move through the xml file and implement the needed rules(template) to the appropriate elements (the match).

Good, that is it for this article, as said, we dealt with the essentials of XSLT, therefore now you are able to generate your own file and produce the contents of an HTML file.

I really should also point out that one of the main plus points of XSLT is the potential to produce multiple views on the same xml file. For example, a single product.xml file that stores the product information, downloads, screenshots, and more could be utilized for a product summary page, the downloads list, a screenshot gallery, and more.

The key advantage of this procedure is that adding or adjusting a product does definitely not mean any presentation changes--only data ones, which is a sizable gain and indeed time and cost saving for any company.

An XSLT processor (like Liquid XSLT Debugger), will unquestionably parse the input XML record, as well as the XSLT style sheet and also subsequently applying the elements from input XML document, process the rules or instructions which will be within the XSLT style sheet. All through the processing of the XSLT instructions, a structured XML output is produced.



















No comments:

Post a Comment