Xml Xsl(eXtendion Style Language),Xsl可以用来控制Xml文件的呈递效果。配合Xml文件中存储的数据能灵活的开发出很多非常有意思的程序。
Xsl语言工作程序是基于模板的,系统提供了一些基础的模板,自己也可以定义自己的模板。假如有这样一个xml文件
<books> <book> <bookid>1</bookid> <author>Jim</author> <price>99.00</price> </book> <publisher> <bookid>1</bookid> <publishername>***</publishername> </publisher> </books>
在根节点books下有2个子节点,现在我们想对这两个节点应用不同的外观效果,我们可以这么做:
<template match="book"> <ul> <li><value-of select="bookid" /></li> <li><value-of select="author" /></li> <li><value-of select="price" /></li> </ul> </template> <template match="publisher"> <table> <tr> <td><value-of select="bookid"/></td> <td><value-of select="publishername"/></td> </tr> </table> </template> <template match="/"> <html> <head></head> <body> <apply-tempates /> </body> </html> </template>
最终效果是:book节点是以无序列表的形式来呈递数据,publisher是一表格的形式来呈递数据。
该xsl文件的执行流程如下:
1 检查跟节点(match=“/" 这里的跟节点不是books,books是根节点下的第一个节点)有没有符合的模板--------找到
2 遇到了<apply-templates/>系统模板,该模板的功能是递归调用当前节点的所有子节点,如果有符合模板的就处理,没有就忽略。
3 递归到book节点,执行template(match="book")----------ok
4 递归到publisher节点,执行template(match="publisher")----------ok
5 递归调用结束,处理结束
match属性里面的值是什么?
match属性里面保存的值是xpath表达式,如果把xml比作数据库,那xpath就是sql语言
系统提供了哪些模板?
<apply-templates> 从当前节点开始,递归调用子节点
<value-of> 获取值的模板
<for-each> 迭代
<copy>
<attribute>生成一个元素的属性
<comment>
<choose> 多条件选择
<when> 多条件选择
<otherwise> 多条件选择
<if> 条件选择
<stylesheet> xsl文档的声明语句
<template>声明模板
<variable> 声明变量
<call-template>调用模板
怎么使用这些系统模板?
<template match="//*"> <ul> <for-each select="book"> <if test=".[@bookid]"> <li><value-of select="bookname"/></li> </if> </for-each> </ul> <ul> <for-each select="publisher"> <choice> <when test=".[@publisherid]"> <li>publisherid:<value-of select="./@publisherid"/></li> </when> <when test=".[@publishername]"> <li>publishername:<value-of select="./@publishername"/></li> </when> <otherwise> <li>other:<value-of select="."/></li> </otherwise> </if> </for-each> </ul> <variable name="myname" select="ghostbear"/> <element name="a"> <attribute name="href"><value-of select="."/></attribute> 联系作者<value-of select="$myname" /> </element> </template>
下面看一个具体的例子:
xml文件:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="exercise.xslt"?> <books> <book id="1"> <bookname>xml programming</bookname> <author>ghostbear</author> <price>199.00</price> </book> <book> <bookname>c# programming</bookname> <author>ghostbear</author> <price>99.00</price> </book> <book id="3"> <bookname>vb.net programming</bookname> <author>ghostbear</author> <price>299.00</price> </book> <book> <bookname>c++.net programming</bookname> <author>ghostbear</author> <price>399.00</price> </book> <book id="5"> <bookname>javascript programming</bookname> <author>ghostbear</author> <price>189.00</price> </book> </books>
xslt文件:
<?xml version="1.0" encoding="GB2312"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:output method="xml" version="1.0" encoding="GB2312" indent="yes"/> <xsl:template match="/"> <html> <head> <title>total test</title> </head> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="books" name="printBook"> <xsl:variable name="author" select="ghostbear"></xsl:variable> <table> <xsl:for-each select="book"> <tr> <xsl:choose> <xsl:when test="./@id"> <td><xsl:value-of select="./@id"></xsl:value-of></td> </xsl:when> <xsl:otherwise> <td>N/A</td> </xsl:otherwise> </xsl:choose> <td><xsl:value-of select="bookname"></xsl:value-of></td> <td><xsl:element name="a"><xsl:attribute name="href">#</xsl:attribute><xsl:value-of select="author"/></xsl:element></td> <td><xsl:value-of select="price"></xsl:value-of></td> <td><xsl:element name="a"><xsl:attribute name="href">#</xsl:attribute>联系作者<xsl:value-of select="$author"/></xsl:element></td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>
495

被折叠的 条评论
为什么被折叠?



