Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XSLT is on the server side


May 28, 2021 XSL T


Table of contents


XSLT - on the server side

You can convert XML to XHTML on a server side, as detailed in this section.

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


Cross-browser solutions

In the previous sections, we explained how to use XSLT in your browser to convert from XML to XHTML. W e created a JavaScript that uses an XML parser to convert. JavaScript solutions do not work in browsers that do not have XML parsers.

In order for XML data to work for any type of browser, we have to convert the XML document on the server and send it back to the browser as XHMTL.

This is another advantage of XSLT. One of XSLT's design goals is 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 files and XSLT files

Take a look at this XML document that was shown in the previous section:

<?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 the 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>

View the XSL file.

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

Important: This sentence above means that XML files can be converted using several different XSL style sheets.


Convert XML to XHTML on the server

This is the source code used to convert XML files to XHTML on the server:

<%
'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 don't know how to write an ASP, you can take our ASP tutorial.

The first piece of code created an instance of Microsoft's XML Parser (XMLDOM) and loaded the XML file into memory. T he second piece of code creates another instance of the parser and loads the XSL file into memory. T he last line of code transforms the XML document using the XSL document and sends the results as XHTML to your browser. That's great!

How it works.

In the following section, we'll show you how to edit XML!