Transforming XML Data with XSLT
We can style XML data using CSS but the standard way to format XML documents is by the use of XSL which stands for Extensible Stylesheet Language. The most important part of XSL is XSLT which stands for XSL Transformations. In addition to XSLT, XSL has two more parts: XPath for the navigation in XML document and XSL-FO for the formatting of XML document.
Here we will
understand the XSLT transformations which can change XML documents into
other formats. In XSLT we deal with templates. Templates are predefined
and a comparison is done between these templates and the source document
whose transformation is needed to be done. If this comparison produces
a match then a transformation of the matching part of source is done by
XSLT into a result document.
For digging into the details of XSL transformations consider the following XML document:
Now assume that we need to transform this XML document into XHTML format. For achieving this transformation we have to create a XSL stylesheet. This will have root element declaration and then a template for doing transformation. The root element is <xsl:transform> or <xsl:stylesheet> and it indicates that the document is a XSL stylesheet. As we know transformation is done when a match is found and it is done on the basis of rules which are contained in the transformation template.
Lets discuss the template elements. Firstly we have <xsl:template> which is used for building templates. It has an attribute match which is used to refer the part of XML document which would be compared with the template. If we are going to match the whole document then value of match attribute will be /. The another element is <xsl:for-each>. It is used to select an XML element and to perform looping in XSLT.
One important element is <xsl:value-of> which extracts the value of an XML element for the transformation purposes. Now we will see how to build an XSL stylesheet with the described elements for the above XML document. Consider the following:
<?xml version="1.0" encoding="ISO-8859-1"?>
So this is an example explaining how we can create our XSL stylesheet. Now in order to accomplish the transformation of above XML document with respect to this stylesheet we need to add a reference of this stylesheet into our XML document. Lets call this stylesheet style1.xsl. Now we need to add the following line in the beginning of our XML document after the XML version and encoding declaration.
<?xml-stylesheet type="text/xsl" href="style1.xsl"?>
| All About Different Types of XML Editors | Generating XML Document Using JSP | How to Retrieve an Email Message Using Java Mail API | How to Send an Email Message Using Java Mail API | Transforming XML Data with XSLT | Understanding Basic Components of a JMS Program | Understanding Basic Technology of ebXML | Understanding Deployment Descriptor in Reference with Servlets | Understanding eXtensible Access Control Markup Language (XACML) | Understanding the Design Goals of XML | Understanding XML Common Biometric Format |
Copyright - © 2004 - 2018 - All Rights Reserved.