详细介绍XSLT模板转换XML文档的代码案例分析
XML文档只能表现数据的内容,而实际的数据则是要呈现在网页中的。使用CSS可以格式化XML文档,使它显示出来,这个内容在上一篇中已经做了详细的说明。除了CSS外,还有一种语言也可以在网页中表现出XML数据内容,那就是XSL。XSL语言包括XSLT(XSL Transformation)和FO(Format Object)。XSLT文档可以将XML文档转化为其它文档形式,如HTML、Text等。FO用于格式化输出,由于W3C对FO还形成统一标准,这里将只说明XSLT的用法。
使用XSLT时最重要的部分是选择XML节点值和创建模板。创建模板使用的标签是<xsl:template></xsl:template>,通常这个标签需要一个match属性,用来确定它所匹配的XML节点。选择XML节点值使用的标签是<xsl:value-of />,这个标签需要select属性来确定它匹配的XML节点。下面将用一个简单的例子说明,看下面的XML文档:
1 <?xml version="1.0" encoding="utf-8"?>2 <?xml-stylesheet type="text/xsl" href="stylesheet.xslt"?>3 <xml>4 <book>5 <name>Xml应用系列</name>6 <author>学路的小孩</author>7 <date>2009-03-23</date>8 </book>9 </xml>
代码说明:第一行是XML文件的声明部分;第二行声明引入XSLT文件,type属性说明文件的类型是text/xsl,href属性指向一个XSLT文件,文件名为stylesheet.xslt。第三行以后为XML文档部分。下面是stylesheet.xslt的内容:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 3 <xsl:template match="/"> 4 <html> 5 <head> 6 <title>第一个XSLT文件</title> 7 </head> 8 <body> 9 <xsl:apply-templates select="xml"></xsl:apply-templates>10 </body>11 </html>12 </xsl:template>13 <xsl:template match="xml"> 14 <table style="background-color:orange">15 <tr>16 <th>书名</th>17 <th>作者</th>18 <th>日期</th>19 </tr>20 <xsl:apply-templates select="book"></xsl:apply-templates>21 </table>22 </xsl:template>23 <xsl:template match="book">24 <tr>25 <td>26 <xsl:value-of select="name"/>27 </td>28 <td>29 <xsl:value-of select="author"/>30 </td>31 <td>32 <xsl:value-of select="date"/>33 </td>34 </tr>35 </xsl:template>36 </xsl:stylesheet>
代码说明:由于XSLT文档的格式依然是XML格式,所以第一行为XML的头部声明;第二行则是XSLT的版本和命名空间声明,并且该标签是XSLT文档的跟节点。第三行使用<xsl:template></xsl:template>创建一个模板,select="/"表示匹配的是文档的根节点。第四行到第十一行是这个节点要生成的HTML节点信息,其中第九行<xsl:apply-templates />标签表示应用模板,其中select="xml"表示要调用的模板为匹配XML节点的模板,这个模板在第十三行出现。后面的所有行(除了第26行等)无非是这些内容的重复,不做太多介绍。第二十六行是选择name标签的内容。使用IE打开XML文件,显示内容如下:
另外,XSLT还具有流程控制、条件选择、循环处理、元素排序等功能。下面通过一个实例来说明,其中XML文档内容如下:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <?xml-stylesheet type="text/xsl" href="bookListStyle.xslt"?> 3 <bookList> 4 <category type="计算机"> 5 <book id="1"> 6 <title>网页与Web程序设计</title> 7 <author>吴利兵</author> 8 <pubInfo> 9 <publisher>机械工业出版社</publisher>10 <pubDate>2009-04-01</pubDate>11 <price>16.50</price>12 </pubInfo>13 </book>14 <book id="2">15 <title>软件工程</title>16 <author>邓良松</author>17 <pubInfo>18 <publisher>西安电子科技出版社</publisher>19 <pubDate>2005-06-10</pubDate>20 <price>33.20</price>21 </pubInfo>22 </book>23 </category>24 <category type="小说">25 <book id="3">26 <title>茶花女</title>27 <author>小仲马</author>28 <pubInfo>29 <publisher>外语出版社</publisher>30 <pubDate>2005-06-30</pubDate>31 <price>22.00</price>32 </pubInfo>33 </book>34 <book id="4">35 <title>红楼梦</title>36 <author>曹雪芹</author>37 <pubInfo>38 <publisher>中国教育出版社</publisher>39 <pubDate>2005-09-06</pubDate>40 <price>55.00</price>41 </pubInfo>42 </book>43 </category>44 </bookList>
bookListStyle.xslt文件的内容如下:
<?xml version="1.0" encoding="UTF-8" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>图书列表</title> <style> <![CDATA[ body,td,th{ font-size:10pt; font-family:宋体; } body{ background-color:#c0c0c0; } table{ border:solid red 1px; margin-left:30px; margin-right:30px; background-color:#ffffc0; cellPadding:4; } ]]> </style> </head> <body> <table> <caption align="top" style="font-weight:bold; text-align:left">图书列表</caption> <tr style="color:#8b0000" align="left"> <th width="5%">编号</th> <th width="10%">类别</th> <th width="25%">书名</th> <th width="20%">作者</th> <th width="25%">出版社</th> <th width="10%">出版日期</th> <th width="5%">定价</th> </tr> <xsl:for-each select="bookList/category/book"> <xsl:sort select="pubInfo/price" order="descending"/> <tr> <xsl:attribute name="style"> color: <xsl:if test="../@type[.='计算机']">blue</xsl:if> </xsl:attribute> <xsl:attribute name="title"> <xsl:value-of select="title"/> <xsl:choose> <xsl:when test="../@type[.='计算机']"> 类别:计算机类图书 </xsl:when> <xsl:otherwise> 类别:小说类图书 </xsl:otherwise> </xsl:choose> 作者:<xsl:value-of select="author"></xsl:value-of> <br/> 出版社:<xsl:value-of select="pubInfo/publisher"/> <br/> 出版日期:<xsl:value-of select="pubInfo/pubDate"/> <br/> 定价:<xsl:value-of select="pubInfo/price"/>元 </xsl:attribute> <td> <xsl:value-of select="@id"/> </td> <td> <xsl:value-of select="../@type"/> </td> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="author"/> </td> <td> <xsl:value-of select="pubInfo/publisher"/> </td> <td> <xsl:value-of select="pubInfo/pubDate"/> </td> <td> <xsl:value-of select="pubInfo/price"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>
这里不再对代码进行分析,请读者自己理解这段代码,并动手写一下自己的XSLT模板。这段代码的运行效果如下图:
更多相关文章
- 使用CSS样式表格式化XML文档的详情介绍
- 详解根据xsd生成xml文档的示例代码分析
- 详解XML命名空间(XML Namespaces)介绍以及节点读取方法的示例代码
- xml创建根节点、子节点的示例代码分享
- 详解在XML文档中替换元素名称的方法(图)
- 详细介绍使用UTF-8对XML文档进行编码
- 使用XSL和ASP在线编辑XML文档的代码详解
- 应用名称访问XML文档的代码案例详解
- 详解xml文档正确格式的示例代码