Server | tutorials
IV: The implementation of XSL---on the server side
1. Compatible with all browsers
In the previous chapter we described the XML parser (parsing software) that can be used to invoke the browser through JavaScript to transform XML documents. But the solution still has a problem: if the browser doesn't have XML
What about the parser plugin? (Note: IE5 with XML parser)
In order for our XML data to be displayed correctly by all browsers, we had to convert the XML to 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.
In the same way, conversion work will be the main work of future server segments.
2. A concrete example
Here is some of the code for an example of an XML document (Cd_catalog.xml) that we 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>
.
.
.
The following is the complete XSL file (cd_catalog.xsl):
<?xml version= ' 1.0 '?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<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>
</xsl:template>
</xsl:stylesheet>
The following is the original code that converts an XML file to an HTML file on the server side:
<%
' Load the XML
Set xml = Server.CreateObject ("Microsoft.XMLDOM")
Xml.async = False
Xml.load (Server.MapPath ("Cd_catalog.xml"))
' Load the XSL
Set xsl = Server.CreateObject ("Microsoft.XMLDOM")
Xsl.async = False
Xsl.load (Server.MapPath ("cd_catalog.xsl"))
Response.Write (Xml.transformnode (XSL))
%>
Note: The examples we use here are ASP files, written in VBScript. If you do not understand ASP or VBScript, suggest reading about books. (Of course, you can also write server-side programs in other languages)
The first code creates a Microsoft Parser (XMLDOM) Resolved object and reads the XML document into memory, the second code creates another object and imports the XSL document, and the last line of code converts the XML document into an XSL document and outputs the results to an HTML file.