您好,欢迎访问一九零五行业门户网

详细介绍XSLT模板转换XML文档的代码案例分析

text-align5db79b134e9f6b82c0b36e0489ee08ed59ee6ffd1dfc9b0d489eaa5e2a418abbe94ab143aef4805f201a19ba8e8a2d4a,通常这个标签需要一个match属性,用来确定它所匹配的xml节点。选择xml节点值使用的标签是7fcf6d841594c6a6506403066462057a,这个标签需要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模板。这段代码的运行效果如下图:
以上就是详细介绍xslt模板转换xml文档的代码案例分析的详细内容。
其它类似信息

推荐信息