当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT - 服务器端
Since not all browsers support XSLT, one solution is to transform the XML to XHTML on the server.
由于并不是所有的浏览器都支持XSLT,所以人们提出了一个解决方案,就是在服务器端把XML转换成XHTML。
In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We created a JavaScript that used an XML parser to do the transformation. The JavaScript solution will not work in a browser that doesn't have an XML parser.
上一章我们解释了怎样在浏览器中使用XSLT把文件从XML转换成XHTML。我们创建了一个JavaScript 程序,它使用XML解析器执行转换任务。JavaScript 如何缺少一个XML解析器,那么它就不能在浏览器中运行。
To make XML data available to all kind of browsers, we must transform the XML document on the SERVER and send it as XHTML back to the browser.
如果想使XML数据支持所有类型的浏览器,我们必须在服务器端转换XML文档,并把它以XHTML的形式传回浏览器。
That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of browsers.
这是XSLT的另外一个魅力所在。创造XSLT的初衷之一就是能够将数据从一个格式转换成另一个格式,并使不同的浏览器支持传回的数据。
Look at the XML document that you have seen in the previous chapters:
下面我们来看一下上一章使用过的XML文件:
<?xml version="1.0" encoding="ISO-8859-1"?> |
And the accompanying XSL style sheet:
附随的XSL样式表:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> </xsl:stylesheet> |
Notice that the XML file does not have a reference to the XSL file.
注意XML文件和XSL文件并无关联
IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL style sheets.
重点: 上面这段话说明了一个XML文件可以通过多个不同的XSL样式表进行转换。
Here is the ASP source code needed to transform the XML file to XHTML on the server:
下面就是在服务器端把XML文件转换成XHTML的ASP源代码:
<% |
Tip: If you don't know how to write ASP, you can study our ASP tutorial.
提示: 如果你不知道怎样书写ASP,你可以了解一下ASP 教程
The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into memory. The second block of code creates another instance of the parser and loads the XSL file into memory. The last line of code transforms the XML document using the XSL document, and sends the result as XHTML to your browser. Nice!
第一块代码创建了一个微软的XML文件解析器(XMLDOM)的实例,并把XML文件加载到内存中;第二块代码创建了另外一个解析器实例,并且把XML文件加载到内存中。最后一行的代码使用XSL文件来转换XML文档,并在浏览器中以XHTML的形式发送结果。太妙了!
See how it works.
看看它到底是怎样工作的.