SoFunction
Updated on 2025-03-02

XSL brief tutorial (4) Implementation of server-side

Original: Jan Egil Refsnes Translation: Ajie

Four: XSL --- Implementation on the server side


1. Compatible with all browsers

In the above chapter, we introduced that XML parser (parser software) can be used to call the browser's XML parser (parser software) to convert XML documents. But there is still a problem with this solution: if the browser does not have XML
What to do with parser plugin? (Note: IE5 comes with XML parser)

In order to enable our XML data to be displayed correctly by all browsers, we had to convert XML into pure HTML code on the server side and then output it to the browser.

This is another benefit of using XSL. Converting one format to another on the server side is also one of the design goals of XSL.

Similarly, conversion work will become the main task of future server segments.


2. A specific example

Here is some code for an example of an XML document (cd_catalog.xml) mentioned above:

<?xml version="1.0" encoding="ISO8859-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>

.

.

.


Here is the complete XSL file (cd_catalog.xsl):


<?xml version='1.0'?>

<xsl:stylesheet xmlns:xsl="http:///TR/WD-xsl">

<xsl:template match="/">

<html>

<body>

<table border="2" bgcolor="yellow">

<tr>

<th>Title</th>

<th>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>


Here is the original code for converting XML files to HTML files on the server side:


<%

'Load the XML

set xml = ("")

= false

(("cd_catalog.xml"))


'Load the XSL

set xsl = ("")

= false

(("cd_catalog.xsl"))


((xsl))

%>


Note: Our example here uses ASP files, written in VBScript. If you don't know ASP or VBScript, it is recommended to read related books. (Of course, you can also use other languages ​​to write server-side programs)


The first piece of code creates an object parsed by Microsoft Parser (XMLDOM) and reads the XML document into memory; the second piece of code creates another object and imports the XSL document; the last line of code converts the XML document into an XSL document and outputs the result to the HTML file.