Original: Jan Egil Refsnes
Iv. XSL-server-side implementation
1. compatible with all browsers
In the previous chapter, we introduced how to use JavaScript to call the XML parser (parsing software) of the browser to convert XML documents. However, this solution still has a problem: If the browser does not have XML
What should I do with the parser plug-in? (Note: XML parser is included in IE5)
In order to make the XML data correctly displayed by all browsers, We have 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 will become the main task of future server segments.
2. A specific instance
The following is part of the code in the above-mentioned XML document (cd_catalog.xml) Example:
<? 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 complete XSL file (cd_catalog.xsl) is as follows ):
<? Xml version = '1. 0'?>
<Xsl: stylesheet xmlns: xsl = "http://www.w3.org/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>
The following is the original code for converting an XML file to an HTML file on the server:
<%
'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 example here uses an ASP file written in VBScript. If you do not know ASP or VBScript, we recommend that you read related books. (Of course, you can also write server programs in other languages)
The first code creates an object parsed by Microsoft Parser (XMLDOM) and reads the XML document into the memory. The second 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 an HTML file.