xsl–转换
本节将举例学习如何用xsl将xml转换成html。这个举例的细节将在下一节中解释。
从xml文档开始
首先从打算转换成html的xml文档开始:
<?xml version="1.0"?>
<catalog>
<cd>
<title>empire burlesque</title>
<artist>bob dylan</artist>
<country>usa</country>
<company>columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
.
如果使用的是inte.net explorer 5.0或更高版本,就可以查看这个xml文件的显示结果。
创建一个xsl样式表文档
现在用转换模板来创建一个xsl样式表:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/tr/wd-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>title</th>
<th>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>
如果使用的是inte.net explorer 5.0或更高版本,就可以查看这个xsl文件的显示结果。
将样式表连接到xml文档
现在向xml文档中增加一个xsl样式表引用:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="cd_catalog.xsl"?>
<catalog>
<cd>
<title>empire burlesque</title>
<artist>bob dylan</artist>
<country>usa</country>
<company>columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
.
如果有一个与xsl兼容的浏览器,例如inte.net explorer 5.0或更高版本,那么就能很好地将xml转换成html。点击这里查看结果。
xsl模板
xsl用模板来描述如何输出 xml。
css的使用规则
如果已经学习过css的知识,我们就会知道css是用一个或多个规则来定义html元素的输出,用一个选择器将规则与一个html元素联系起来。比如以下这个css规则中的p选择器说明应该用一种叫做arial的字体来显示一个<p>元素:
p { font-family: arial }
xsl使用模板
xsl使用一个或多个模板来定义如何输出xml元素,用一个匹配属性来将模板与一个xml元素联系起来,还可以用匹配属性来为xml文档的一个完整分支来定义模板。
请看以下的xsl样式表,它包含一个模板以输出前一节中的xml cd目录:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/tr/wd-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>title</th>
<th>artist</th>
</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
由于样式表本身就是一个xml文档,因此文档以一个xml声明开始:<?xml version='1.0'?>。第二行中的xsl:stylesheet标记定义了样式表的开始。第三行中的xsl:template标记定义了一个模板的开始。模板属性match="/"将模板与xml源文档的根 (/)联系(匹配)起来。文档的其它部分包含了模板本身,最后两行定义了模板的结束和样式表的结束。
用inte.net explorer 5来看看xml文件、xsl文件以及结果。
<xsl:value-of>元素
前面例子的结果有点令人失望,因为没有将数据从xml文档复制到输出中。xsl的<xsl:value-of>元素可以用来选择进入xsl转换输出流中的xml元素:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/tr/wd-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>title</th>
<th>artist</th>
</tr>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
注意:选择属性值用到的语法被称为xsl模式。它工作起来就象是在一个文件系统中航行,其中用一个前斜线 (/) 来选择子目录。
用inte.net explorer 5来看看xml文件、xsl文件以及结果。
<xsl:for-each>元素
前面例子中的结果还是有点不太令人满意,因为从xml文档中只复制了一行数据到输出。xsl的<xsl:for-each>元素可以用来将每个xml元素选择到xsl转换的输出流中:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/tr/wd-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>title</th>
<th>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>
xsl:for-each元素在xml文档中查找元素,然后为每个元素重复模板的一部分。
用inte.net explorer 5来看看xml文件、xsl文件以及结果。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器