XSLT Transformation

In this tutorial, we will learn about how to transform XML into XHTML using XSLT.

Correct StyleSheet Declaration:

To declare the document to be an XSL style sheet, <xsl:stylesheet> or <xsl:transform> are used as the root element. Being completely synonymous, either of them can be used. According to the W3C XSLT Recommendation, the correct way to declare an XSL style sheet is:

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

Or,

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

The XSLT namespace should be declared at the top of the document, to get access to the XSLT elements, attributes and features. To point to the official W3C XSLT namespace, the xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” namespace is used. For this, the attribute version=”1.0″, must also be included.

Start with a Raw XML Document:

Books.xml:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Author Name</author>
    <year>2020</year>
    <price>100.00</price>
  </book>
 
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <year>2020</year>
    <price>300.00</price>
  </book>
 
</bookstore>

Viewing XML Files in IE, Chrome, Firefox, Safari, and Opera: On opening the XML file, it will be probably displayed with colour-coded root and child elements. To expand or collapse the element structure, we can click the plus (+) or minus sign (-), present to the left of the elements.

Create an XSL Style Sheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>List of Books</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Title</th>
      <th style="text-align:left">Price</th>
    </tr>
    <xsl:for-each select="bookstore/book">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="price"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Explanation:

Here, we are creating an XSL Style Sheet (“books.xsl”) with a transformation template.

Link the XSL Style Sheet to the XML Document:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<bookstore>
 
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Author Name</author>
    <year>2020</year>
    <price>100.00</price>
  </book>
 
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <year>2020</year>
    <price>300.00</price>
  </book>
 
</bookstore>

Explanation:

Now, we are adding the XSL style sheet reference to our XML document (“books.xml”). To nicely transform an XML into XHTML, an XSLT compliant browser should be used.

 

Please Share