To make XML data available to all kind of browsers, we can transform the XML document on the SERVER and send it back to the browser as XHTML.
A Cross Browser Solution
In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We used a JavaScript and an XML parser for the transformation. However, this will not work in a browser that doesn't have an XML parser.
To make XML data available to all kind of browsers, we can transform the XML document on the server and send back to the browser as XHTML.
That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of browsers.
The XML File and the XSLT File
Look at the XML document that you have seen in the previous chapters:
<?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>
And the accompanying 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>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>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Notice that the XML file does not have a reference to the XSL file.
IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL style sheets.
PHP Code: Transform XML to XHTML on the Server
Here is the PHP source code needed to transform the XML file to XHTML on the server:
<?php
// Load XML file
$xml = new DOMDocument;
$xml->load('cdcatalog.xml');
// Load XSL file
$xsl = new DOMDocument;
$xsl->load('cdcatalog.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
// Attach the xsl rules
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
?>
See how it works with PHP:
My CD Collection
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |
Hide your heart | Bonnie Tyler |
Greatest Hits | Dolly Parton |
Still got the blues | Gary Moore |
Eros | Eros Ramazzotti |
One night only | Bee Gees |
Sylvias Mother | Dr.Hook |
Maggie May | Rod Stewart |
Romanza | Andrea Bocelli |
When a man loves a woman | Percy Sledge |
Black angel | Savage Rose |
1999 Grammy Nominees | Many |
For the good times | Kenny Rogers |
Big Willie style | Will Smith |
Tupelo Honey | Van Morrison |
Soulsville | Jorn Hoel |
The very best of | Cat Stevens |
Stop | Sam Brown |
Bridge of Spies | T`Pau |
Private Dancer | Tina Turner |
Midt om natten | Kim Larsen |
Pavarotti Gala Concert | Luciano Pavarotti |
The dock of the bay | Otis Redding |
Picture book | Simply Red |
Red | The Communards |
Unchain my heart | Joe Cocker |
ASP Code: Transform XML to XHTML on the Server
Here is the ASP source code needed to transform the XML file to XHTML on the server:
<%
'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))
%>
See how it works with ASP:
My CD Collection
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |
Hide your heart | Bonnie Tyler |
Greatest Hits | Dolly Parton |
Still got the blues | Gary Moore |
Eros | Eros Ramazzotti |
One night only | Bee Gees |
Sylvias Mother | Dr.Hook |
Maggie May | Rod Stewart |
Romanza | Andrea Bocelli |
When a man loves a woman | Percy Sledge |
Black angel | Savage Rose |
1999 Grammy Nominees | Many |
For the good times | Kenny Rogers |
Big Willie style | Will Smith |
Tupelo Honey | Van Morrison |
Soulsville | Jorn Hoel |
The very best of | Cat Stevens |
Stop | Sam Brown |
Bridge of Spies | T`Pau |
Private Dancer | Tina Turner |
Midt om natten | Kim Larsen |
Pavarotti Gala Concert | Luciano Pavarotti |
The dock of the bay | Otis Redding |
Picture book | Simply Red |
Red | The Communards |
Unchain my heart | Joe Cocker |