xsl索引
xsl可以用来对一个xml文档进行索引。
将索引信息放在哪里
现在重新看看在以前许多章节中都曾看到过的 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>
.
.
.
要想将这个xml文件作为一个普通的html文件输出,并且同时对它进行索引,只需要在xsl文件中增加一个order-by 属性,如下:
<xsl:for-each select="catalog/cd" order-by="+ artist">
order-by属性使用加号(+)或减号(-)来定义是使用升序还是降序,再用一个元素名称来定义排序的元素。
现在来看看经过轻微调整的xsl样式表(或在ie5中打开它):
<?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"
order-by="+ artist">
<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>
在浏览器中转换
以下是在浏览器中将xml文件转换成html所需要的简单代码:
<html>
<body>
<script language="javascript">
// load xml
var xml = new activexobject("microsoft.xmldom")
xml.async = false
xml.load("cd_catalog.xml")
// load the xsl
var xsl = new activexobject("microsoft.xmldom")
xsl.async = false
xsl.load("cd_catalog_sort.xsl")
// transform
document.write(xml.transformnode(xsl))
</script>
</body>
</html>
如果使用的是inte.net explorer 5.0 或更高版本,请点击这里查看结果。
xsl过滤器查询
xsl可以用来过滤一个 xml 文件。
在哪里放置过滤器信息
现在重新看看你以前已经看过多次的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>
.
.
.
要过滤xml文件,只需要为xsl文件中的for-each元素的选择属性增加一个过滤器,如下:
<xsl:for-each select="catalog/cd[artist='bob dylan']">
合法的过滤器操作符是:
= 等于
!= 不等于
< 小于
> 大于
现在看看经过轻微调整的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[artist='bob dylan']">
<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>
在浏览器中转换
以下是在浏览器中将xml文件转换成html所需要的简单代码:
<html>
<body>
<script language="javascript">
// load xml
var xml = new activexobject("microsoft.xmldom")
xml.async = false
xml.load("cd_catalog.xml")
// load the xsl
var xsl = new activexobject("microsoft.xmldom")
xsl.async = false
xsl.load("cd_catalog_filter.xsl")
// transform
document.write(xml.transformnode(xsl))
</script>
</body>
</html>
如果使用的是inte.net explorer 5.0 或更高版本,请点击这里查看结果。
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 注册表 操作系统 服务器 应用服务器