Friday, December 28, 2007

Using XSL to display XML data into a human-readable format

Abstract

When we open our XML document on browser, we will notice only the elements of that XML document. If we want to display our XML data in a table like format then we need to know about XSL.

HTML

HTML contains the predefined tags like <TABLE>, <H1>, etc. This will be well understood by the browser. And the browser also knows how to display these tags.

XML & XSL

The most recent version of XSLT is XSLT 2.0.

XML does not use any predefined tags i.e., we can use any tag-name we like. And these names are not well understood. The browser doesn’t know how to display these tags. Now the XSL plays its role, it stands for “Extensible Stylesheet Language”, which describes how the XML document should be displayed on the browser.Have a look at the following XML Document, which describes about Employees. It contains reference to the XSL file (Employee.xsl).

Employee.xml:



Now we need to create a XSL file, by name “Employee.xsl”.

Employee.xsl:




In the above code, xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. The is used to build a template, and match attribute is used to associate a template with an XML element. Use “/” to associate the whole XML document.

which associates the whole XML document to the template. The used to select every XML element of a specified node-set: in our example “Employees/Employee”. The used to extract the value of an XML element. It is a simple XSL style sheet, there are other elements of XSL (if, choose, sort…), which are also very useful for transformation.Now the browser will nicely transform the XML document to XHTML.Open the “Employee.xml” in your browser.


XML data will be displayed in a table format. XSL also used to transform XML documents into some other formats.