当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT - 服务器端

XSL/XSLT
XSL 语言
XSLT 介绍
XSLT 浏览器
XSLT 转换
<xsl:template>
<xsl:value-of>
<xsl:for-each>
<xsl:sort>
<xsl:if>
<xsl:choose>
<xsl:apply-templates>
XSLT - 客户端
XSLT - 服务器端
XSLT - 编辑XML
XSLT 摘要
XSLT 元素参考
XSLT 函数
XSL 编辑器

XSL/XSLT 中的 XSLT - 服务器端


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 472 ::
收藏到网摘: n/a

Since not all browsers support XSLT, one solution is to transform the XML to XHTML on the server.
由于并不是所有的浏览器都支持XSLT,所以人们提出了一个解决方案,就是在服务器端把XML转换成XHTML。


A Cross Browser Solution
一个跨浏览器解决方案

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的初衷之一就是能够将数据从一个格式转换成另一个格式,并使不同的浏览器支持传回的数据。


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

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 on the Server
在服务器端把XML转换成XHTML

Here is the ASP source code needed to transform the XML file to XHTML on the server:
下面就是在服务器端把XML文件转换成XHTML的ASP源代码:

<%
'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 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.
看看它到底是怎样工作的.

评论 (0) All

登陆 | 还没注册?