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:


Or,


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:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2020
    300.00
  


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:




 

  

List of Books

Title Price

Explanation:

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

Link the XSL Style Sheet to the XML Document:





  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2020
    300.00
  


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.