XSLT - On the Server Side


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


Cross-browser Solution

In the previous chapter, we explained how to use XSLT in the browser to convert XML to XHTML. We created a JavaScript that uses an XML parser to perform the conversion. The JavaScript solution does not work in browsers without an XML parser.

To make XML data usable in any type of browser, we must convert 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 was to make it possible to convert data from one format to another on the server and return readable data to all types of browsers.


XML File and XSLT File

Look at this XML document that was shown in a previous chapter:

<?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 the XML file。

And the accompanying XSL stylesheet:

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

<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>
    <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 the XSL file。

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

Important:The sentence above means that the XML file can be converted using multiple different XSL stylesheets.


Convert XML to XHTML on the Server

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

Convert using PHP code:

<?php
// 载入 XML 文件
$xml = new DOMDocument;
$xml->load('cdcatalog.xml');

// 载入 XSL 文件
$xsl = new DOMDocument;
$xsl->load('cdcatalog.xsl');

// 设置转换
$proc = new XSLTProcessor;

// 添加 xsl 规则
$proc->importStyleSheet($xsl);

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

Tip:If you don't know how to write PHP, you can study ourPHP Tutorial。

Convert using ASP code:

<%
'载入 XML 文件
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("cdcatalog.xml"))

'载入 XSL 文件
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("cdcatalog.xsl"))

'转换文件
Response.Write(xml.transformNode(xsl))
%>

Tip:If you don't know how to write ASP, you can study ourASP Tutorial。

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

How It Works。


Other Extensions