XSLT - On Server
- Previous Page XSLT on Client Side
- Next Page XSLT Edit XML
Since not all browsers support XSLT, another solution is to complete the transformation from XML to XHTML on the server.
Cross-browser solution
In the previous chapters, we explained how to use XSLT in browsers to complete the transformation from XML to XHTML. We created a JavaScript that uses an XML parser for the transformation. The JavaScript solution does not work in browsers without an XML parser. In order to make XML data applicable to any type of browser, we must convert the XML document on the server and then send it to the browser as XHMTL.
This is another advantage of XSLT. One of the design goals of XSLT 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 file and XSL file
Please see this XML document that has been demonstrated in the previous chapters:
<?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>
As well as the accompanying XSL stylesheet:
<?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>
Please note that this XML file does not contain references to the XSL file.
Important Note:The above statement indicates that an XML file can be transformed using multiple different XSL style sheets.
Converting XML to XHTML on the server
This is the source code for converting an XML file 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 are not familiar with how to write ASP, you can learn from ourASP Tutorial》
The first piece of code creates an instance of Microsoft's XML parser and loads the XML file into memory. The second piece of code creates another instance of the parser and loads the XSL file into memory. The last line of code uses the XSL document to transform the XML document and sends the result as XHTML to your browser. Task completed!
- Previous Page XSLT on Client Side
- Next Page XSLT Edit XML