XSLT – On the Server

The XML document is transformed on the SERVER and is sent back to the browser as XHTML, to make XML data available to all kinds of browsers.

A Cross Browser Solution:

To transform a document from XML to XHTML in the browser, XSLT can be used. Here, a JavaScript and an XML parser are used for the transformation but are not supported by a browser with no XML parser. To overcome this limitation, the XML document is transformed, on the SERVER and, is sent back to the browser as XHTML. We can consequently transform data from one format to another using XSLT, also returning readable data to all kinds of browsers.

The XML File and the XSLT File:

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>2005</year>
    <price>300.00</price>
  </book>
 
  <book category="Sociology">
    <title lang="en">Sociology 1</title>
    <author>Author Name</author>
 <year>2010</year>
    <price>250.00</price>
  </book>
 
  <book category="GK">
    <title lang="en">Current Affairs</title>
    <author>Author Name</author>
 <year>2004</year>
    <price>500.00</price>
  </book>
 
  <book category="Science">
    <title lang="en">Science Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <author>Author 3</author>
 <year>2011</year>
    <price>150.00</price>
  </book>
 
</bookstore>

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="#00FFFF">
      <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:

In the above XML file, there is no reference to the XSL file. It can thus be concluded that an XML file could be transformed using many different XSL style sheets.

PHP Code: Transform XML to XHTML on the Server:

<?php
// Load XML file
$xml = new DOMDocument;
$xml->load('books.xml');
 
// Load XSL file
$xsl = new DOMDocument;
$xsl->load('books.xsl');
 
// Configure the transformer
$proc = new XSLTProcessor;
 
// Attach the xsl rules
$proc->importStyleSheet($xsl);
 
echo $proc->transformToXML($xml);
?>

Explanation:

In the above example, we are writing the PHP source code to transform the XML file to XHTML on the server.

ASP Code: Transform XML to XHTML on the Server:

<%
'Load XML file
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("books.xml"))
 
'Load XSL file
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("books.xsl"))
 
'Transform file
Response.Write(xml.transformNode(xsl))
%>

Explanation:

In the above example, we are writing the ASP source code to transform the XML file to XHTML on the server.

Please Share