header1.html

Sunday 20 July 2014

What is XSLT?How to Transform XML data using XSLT?

In this post we are going to discuss about-How we can display XML content?There are multiple ways to do to so,but XSLT is best suitable way.So,what is XSLT?

To know about XSLT: Extensible Stylesheet Language Transformation, we should have the basic idea of following,

What is a Markup Language?

A language specially designed for processing, defining and presenting text/data is called a Markup Language.

To know about XSLT, we should have the basic idea of following.

There are three languages in XSL family,

  1. XSL Transformations(XSLT): XML language for transforming XML Documents.
  2. XSl  Formatting Objects (XSL-FO):XML language for specifying the visual formatting of an XML document.
  3. XML Path Language (XPATH): a non XML language used by XSLT, and also available for use in non-XSLT contexts, for addressing the parts on an XML document.

  • XSLT helps to transform one XML document to another. Output of XSL transformation can be another XML, HTML or PDF. Data from one XML can be read and modified into another applying arithmetic, numeric, string operations based on the need.
  • XSLT was developed by W3C especially for XML. XSLT internally uses XPATH and XSL-FO for navigating, performing calculation and for formatting the data.
  • XSLT documents are well-formed XML documents that describe how another XML document should be transformed. For XSLT to work, it needs an XML document to transform and an engine to make the transformation take place. In addition, parameters can be passed in to XSLTs providing further instructions on how to do the transformation.
Why and where is it used?

In web application, web services sent the business data in form of XML within the Applications and across the applications. Since XML is accepted as the most common mode of storing and passing data, a language is needed to read the XML in different ways for different application.


A medium like XSLT is needed to parse the data from XML and give it to browsers to render the HTML code. XSLT works in between XML and Browser to transform the data.


Example:

Blog.xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="BlogsStyle.xsl"?>
<Blogs>
       <name id="21">
                <bname>Career Web Helper</bname>
                <Topics>Online Quizzes,MCQs and Articles on Microsoft Technologies.</Topics>
       </name>
                   <place>Pune</place>
                   <language>English</language>
</Blogs>

BlogsStyle.xsl

<?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" indent="yes" omit-xml-declaration="yes" encoding="utf-8"/>
<xsl:template match="Blogs">
<html>
<body>
   <h2>Blog Detail</h2>
   <table border="1">
     <tr>
       <td bgcolor="gray">Blog Id</td>
       <td><xsl:value-of select="name/@id"/></td>
     </tr>
     <tr>
       <td bgcolor="gray">Blog Name</td>
       <td><xsl:value-of select="name/bname"/></td>
     </tr>
      <tr>
       <td bgcolor="gray">Topics Covered</td>
       <td><xsl:value-of select="name/Topics"/></td>
     </tr>
     <tr>
       <td bgcolor="gray">Place</td>
       <td><xsl:value-of select="place"/></td>
     </tr>
     <tr>
       <td bgcolor="gray">Language</td>
       <td><xsl:value-of select="language"/></td>
     </tr>
   </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Output:

Sample Basic XSLT Example

This is only basic of XSLT,we will cover some advance topics in upcoming posts. Hope you like this post.