Latest web development tutorials

XSLT on the server side

Because not all browsers support XSLT, another solution is to complete the conversion to XML on the server of XHTML.


Cross-browser solution

In the previous section, we explain how to use XSLT through the browser to complete the XML to XHTML conversion. We created some use an XML parser to convert the JavaScript. JavaScript solution does not work in no XML parser browser.

To make XML data applicable to any type of browser, we must be on the server for XML document conversion, and then sent back to the browser as XHMTL.

This is another advantage of XSLT. One of the design goals for XSLT was to make data on the server to convert from one format to another format as possible, to all types of browsers returned readable data.


XML documents and XSLT files

Look at this in the previous section has shown off an XML document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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 accompanying XSL style sheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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 align="left">Title</th>
<th 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>

See XSL file .

Please note, the XML file does not contain a reference to the XSL file.

IMPORTANT: The above sentence means, XML files can use several different XSL stylesheets to convert.


On the server convert XML to XHTML

It is used on the server to XML files into XHTML source code:

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

'Load XSL
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 do not know how to write ASP, you can study our ASP tutorial .

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

How it works .