XSLT - 位于客户端

来源:互联网 发布:java主流框架 编辑:程序博客网 时间:2024/04/30 21:22

If your browser supports it, XSLT can be used to transform the document to XHTML in your browser.
如果你的浏览器支持XSLT,它可以在你的浏览器中把文件转换成XHTML。


A JavaScript Solution
JavaScript解决方案

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,我们可以:

  • do browser-specific testing
    做浏览器的细节(browser-specific)测试
  • use different style sheets according to browser and user needs
    根据浏览器和使用者的需要使用不同的样式表。

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的转换一定是未来浏览器工作任务的主要部分,我们将会在专业的浏览器市场看到它的成长(与盲人对应的听觉浏览器, 网络打印机, 手动驱动等等)。


The XML File and the XSL File
XML文件和XSL文件

Look at the XML document that you have seen in the previous chapters:
来看一下已经在前一章看过的XML文件。

<?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>

View the XML file.
XML文件.

And the accompanying XSL style sheet:
附随的XSL样式表:

<?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>

View the XSL file.
查看XSL文件.

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样式表进行转换。


Transforming XML to XHTML in the Browser
在浏览器中把XML文件转换成XHTML

Here is the source code needed to transform the XML file to XHTML on the client:
这儿的源代码需要在客户端把XML文件转换成XHTML。

<html>
<body>
<script type="text/javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cdcatalog.xml")
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cdcatalog.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>

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中的运行情况。