当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT - 客户端
If your browser supports it, XSLT can be used to transform the document to XHTML in your browser.
如果你的浏览器支持XSLT,它可以在你的浏览器中把文件转换成XHTML。
In the previous chapters we have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation.
在前一章中,我们已经解释了XSLT是如何把一份XML文档转换成XHTML的。我们通过向XML文件中添加一个XSL样式表,从而浏览器执行转换任务。
Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.)
即使这样做运行得不错,你也绝不可以放心地将其视为一个XML文件的样式参考(比如,在不支持XSLT的浏览器中,它将不会运行)。
A more versatile solution would be to use a JavaScript to do the transformation.
更通用的解决方法是使用JavaScript 执行转换。
By using a JavaScript, we can:
通过使用JavaScript,我们可以:
That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.
这就是XSLT的魅力。创造XSLT的初衷之一就是能够将数据从一个格式转换成另一个格式,并且支持不同的浏览器和不同的使用者的具体需求。
XSLT transformation on the client side is bound to be a major part of the browsers work tasks in the future, as we will see a growth in the specialized browser market (Braille, aural browsers, Web printers, handheld devices, etc.)
在客户端执行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 source code needed to transform the XML file to XHTML on the client:
下面这段源代码需要在客户端执行XML文件到XHTML文件的转换:
<html> <script type="text/javascript"> // Load XML // Load XSL // Transform </script> </body> |
Tip: If you don't know how to write JavaScript, you can study our JavaScript tutorial.
提示: 如果你不知道JavaScript 如何书写,你可以看一下我们的JavaScript 教程。
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 displays the result as XHTML in your browser. Nice!
第一块代码创建了一个微软的XML文件解析器(XMLDOM)的实例,并把XML文件加载到内存中;第二块代码创建了另外一个解析器实例,并且把XML文件加载到内存中。最后一行的代码使用XSL文件来转换XML文档,并在浏览器中以XHTML的形式显示结果。太妙了!
See how it works in IE.
了解上述代码在IE中的运行情况。