Easy Tutorial
❮ Xsl Choose El With Param ❯

XSLT - On the Server Side


Since not all browsers support XSLT, an alternative solution is to perform the conversion from XML to XHTML on the server.


Cross-Browser Solution

To make XML data suitable for any type of browser, we must perform the transformation of the XML document on the server and then send it back to the browser as XHTML.

This is another advantage of XSLT. One of the design goals of XSLT is to enable data to be converted from one format to another on the server and return readable data to all types of browsers.


XML File and XSLT File

Consider this XML document, which has been shown in previous sections:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
.
.
</catalog>

View XML File.

And the accompanying XSL stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

&lt;xsl:template match="/">
  <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Title</th>
      <th style="text-align:left">Artist</th>
    </tr>
    &lt;xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title" /></td>
      <td><xsl:value-of select="artist" /></td>
    </tr>
    </xsl:for-each>
  </table>
</xsl:template>

</xsl:stylesheet>

View XSL File.

Note that this XML file does not include a reference to the XSL file.

Important: This means that the XML file can be transformed using multiple different XSL stylesheets.


Converting XML to XHTML on the Server

Here is the source code for converting an XML file to XHTML on the server:

Using PHP Code:

<?php
// Load XML file
$xml = new DOMDocument;
$xml->load('cdcatalog.xml');

// Load XSL file
$xsl = new DOMDocument;
$xsl->load('cdcatalog.xsl');

// Set up the transformation
$proc = new XSLTProcessor;

// Attach the xsl rules
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);
?>

Tip: If you are not familiar with writing PHP, you can learn from our PHP Tutorial.

Using ASP Code:

<%
'Load XML file
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("cdcatalog.xml"))

'Load XSL file
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("cdcatalog.xsl"))

'Transform file
Response.Write(xml.transformNode(xsl))
%>

Tip: If you are not familiar with writing ASP, you can learn from our ASP Tutorial.

The first block of code creates an instance of Microsoft's XML parser (XMLDOM) and loads the XML file into memory. The second block of code creates another instance of the parser and loads the XSL file into memory. The last line of code transforms the XML document using the XSL document and sends the result as XHTML to your browser. Great!

How It Works.

❮ Xsl Choose El With Param ❯