bookinfo.xsl
<?xml version="1.0" encoding="UTF-8"?>
<!-- xmlns:xsl :当前文档的标记来自 http://www.w3.org/1999/XSL/Transform命名空间
通过xsl把xml转换成html TR/WD-xsl
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- xsl:template: match="/":指xml文档 -->
<xsl:template match="/">
<html>
<head>
<title>书籍信息</title>
</head>
<body>
<center>
通过xsl把xml转换成html
<hr color="blue"/>
<!--调用指定的template select:查询当前文档中定义的template -->
<xsl:apply-templates select="bookinfo" />
</center>
</body>
</html>
</xsl:template>
<!-- 通过table组织xml中的信息-->
<xsl:template match="bookinfo">
<table border="2">
<caption>书籍信息</caption>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>年龄</th>
<th>类型</th>
<th>操作</th>
<!--循环输出书籍信息 for-each -->
<xsl:for-each select="book" order-by="-@id">
<!--order-by:排序 + - 排序的条件 -->
<tr>
<td><xsl:value-of select="@id" /></td>
<td><xsl:value-of select="bookname" /></td>
<td><xsl:value-of select="author" /></td>
<td><xsl:value-of select="price" /></td>
<td><xsl:value-of select="age" /></td>
<td><xsl:value-of select="type" /></td>
<td>
<!--test:表达式 = != >= <=
属性: @属性名字条件值 内容: 标记名字[.条件值]
TR/WD-xsl 属性和内容检查是一致的
-->
<!--
<xsl:if test="@id>'102'">
<font color="red">价格太贵</font>
</xsl:if>
<xsl:if test="price[.>=90][.<=200]">
<font color="red">价格太贵</font>
</xsl:if> -->
<!--TR/WD-xsl命名空间 属性 -->
<xsl:if test="@id[.>'102']">
<font color="yellow">价格太贵</font>
</xsl:if>
<!--分支结构
<xsl:choose>
<xsl:when test="price[.>=90][.<=200]">
<font color="red">价格太贵</font>
</xsl:when>
<xsl:when test="price[.>50][.<=90]">
<font color="blue">价格正好</font>
</xsl:when>
<xsl:otherwise>
<font color="#abcdef">价格便易</font>
</xsl:otherwise>
</xsl:choose>-->
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
bookinfo.xml
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="bookinfo.xsl"?>
<bookinfo>
<book id="101">
<bookname>java编程思想</bookname>
<author>张三</author>
<price>90</price>
<age>18</age>
<type>喜剧</type>
</book>
<book id="102">
<bookname>7天精通炒股</bookname>
<author>周星星</author>
<price>200</price>
<age>18</age>
<type>悲剧</type>
</book>
<book id="103">
<bookname>21天精通j2ee</bookname>
<author>张A</author>
<price>50.5</price>
<age>18</age>
<type>喜剧</type>
</book>
<book id="104">
<bookname>struts2应用开发</bookname>
<author>周C</author>
<price>40.5</price>
<age>18</age>
<type>喜剧</type>
</book>
</bookinfo>
转载于:https://blog.51cto.com/baiyan425/612470