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:

<?xml version="1.0" encoding="ISO-8859-1"?>
<customer>
<personalinfo>
<name>S.J Odams</name>
<age>35</age>
<gender>male</gender>
<mobile>9910220898</mobile>
</personalinfo>
</customer>

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.

Let’s 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"?>

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

<xsl:template match="/">
<html>
<body>
<b><h3>Customer Personal Information<h3></b>
<table>
<tr>
<th>name</th>
<th>age</th>
<th>gender</th>
</tr>
<xsl:for-each select="customer">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
<td><xsl:value-of select="gender"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

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. Let’s 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"?>

So this is the basic way of creating an XSL stylesheet. It should be noted here that performing this transformation by using an external stylesheet reference in the XML document is possible when our browser supports XSLT. In order make XSLT compatible between different browsers we need a different solution. One is to do XSL transformation on client side by using JavaScript code.

If our browser has XML parser then JavaScript can also be used to convert XML into XHTML document in the browser i.e. on the client side but if it does not have XML parser then we can also do this transformation on server and after that results are sent back to the browser.


.

SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



| 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 |

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.