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:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
 2005
    300.00
  

  
    Sociology 1
    Author Name
 2010
    250.00
  

  
    Current Affairs
    Author Name
 2004
    500.00
  

  
    Science Book
    Author 1
    Author 2
    Author 3
 2011
    150.00
  


XSL style sheet:




 

  

List of Books

Title Price

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:

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.