XMLThe document can only represent the content of the data, while the actual data is to be presented on the web page. You can use CSS to format the XML document so that it can be displayed. This content has been explained in detail in the previous article. . In addition to CSS, there is also a language that can also express XML data content in web pages, that is XSL. XSL Languages include XSLT (XSL Transformation) and FO(Format Object). The XSLT document can convert the XML document into other document forms, such as HTML , Text, etc. FO is used for formatted output, because W3C also forms a unification for FO Standard, here we will only explain the usage of XSLT.
The most important part when using XSLT is choosing XMLNode values and creation templates. The tag used to create a template is
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>
## Code description: The first line is the XML file Declaration part; the second line declares the introduction of the XSLT file, and the type attribute indicates that the type of the file is text/ xsl, href attributes point to a XSLT file named stylesheet. xslt. The third line and onwards are the XML document part. The following is the content of 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>
Code description: Since the format of the XSLT document is still in the XML format, the first line is ## The header declaration of #XML; the second line is the version of XSLT and the namespace declaration, and the tag is XSLTThe following node of the document. The third line uses
另外,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模板。这段代码的运行效果如下图:
The above is the detailed content of Detailed introduction to code case analysis of XSLT template conversion to XML documents. For more information, please follow other related articles on the PHP Chinese website!