xml之XSLT

 1、XSLT是什么

 XSLT是XSL的子集,XSL是样式表。XSLT的作用将XML文档转化成HTML,做的是中间转换者

 而主要需要学习的是XSLT(XSLTransformation)。

 

 2、转换过程

 

3、XSL样式表的表的结构

 

引用XSL样式的XML文件的引用方式:

 

 

4、XSLT详细结构

1》有独立的命名空间

 

2》要执行XSLT文件,需要以下组件

    XML 文档

    XSL 样式表

    XSLT 处理器

 

3》XSLT 处理器是一个将 XSLT 样式表连接到 XML 文档的应用程序

 

5、XSLT模板

书写规则:

模板的调用:

 

6、XPath介绍

在template 的match 匹配的时候,使用的就是 XPath 匹配

1》按名称匹配元素

2》按父子元素匹配元素

3》按元素匹配元素

4》按子元素匹配元素

5》通配符匹配规则

 

7、XSLT元素

 

8、XSLT实例

xml文件:

 1 <orderdetails>
 2   <order>
 3     <number>0001</number>
 4     <item-info>
 5       <item-name>计算器</item-name>
 6       <price>20</price>
 7       <quantity>5</quantity>
 8     </item-info>
 9 
10     <item-info>
11       <item-name>记事本</item-name>
12       <price>5</price>
13       <quantity>20</quantity>
14     </item-info>
15 
16   </order>
17   <order>
18     <number>0002</number>
19     <item-info>
20       <item-name>钢笔</item-name>
21       <price>5</price>
22       <quantity>20</quantity>
23     </item-info>
24 
25     <item-info>
26       <item-name>铅笔</item-name>
27       <price>2</price>
28       <quantity>35</quantity>
29     </item-info>
30 
31   </order>
32 </orderdetails>
xml文件代码

 

 

实现效果:

XSLT文件及注释(for-each实现):

 1   <!--使用循环的方式完成-->
 2   <xsl:template match="orderdetails">
 3     <HTML>
 4       <HEAD>
 5         <TITLE>使用xslt</TITLE>
 6       </HEAD>
 7       <BODY>
 8         <xsl:for-each select="order">
 9           <H1>
10             <xsl:text>订单号:</xsl:text>
11             <xsl:value-of select="number"/>
12           </H1>
13           <xsl:for-each select="item-info">
14             <!--首先要定义变量:价格,留着下面进行调用-->
15             <xsl:variable name="x" select="price"></xsl:variable>
16             <H3>
17               <!--xslt中内置的函数 position():获取当前的位置(在循环中的位置)-->
18               <xsl:value-of select ="position()"/>
19               <xsl:value-of select="item-name"/>
20             </H3>
21 
22             <!--下面计算商品的价格-->
23             <H4>
24               <xsl:text>商品的价格:</xsl:text>
25               <!--调用定义的变量-->
26               <xsl:value-of select="$x"/>
27             </H4>
28 
29           </xsl:for-each>
30         </xsl:for-each>
31       </BODY>
32     </HTML>
33     
34   </xsl:template>
XSLT文件代码(循环)

 

 

XSLT文件及注释(template调用):

 1   <xsl:template match="orderdetails">
 2     <HTML>
 3       <HEAD>
 4         <TITLE>使用动态模板调用的方式</TITLE>
 5       </HEAD>
 6       <BODY>
 7         <xsl:apply-templates select="order"></xsl:apply-templates>
 8       </BODY>
 9     </HTML>
10   </xsl:template>
11 
12   <xsl:template match="order">
13     <H1>
14       <xsl:text>订单号:</xsl:text>
15       <xsl:value-of select="number"/>
16     </H1>
17     <xsl:apply-templates select="item-info"/>
18   </xsl:template>
19 
20   <xsl:template match="item-info">
21     <!--要计算:首先应先定义变量-->
22     <xsl:variable name="p" select="price"/>
23     <xsl:variable name="q" select="quantity"/>
24     <H3>
25       <xsl:text>商品名称:</xsl:text>
26       <xsl:value-of select="item-name"/>
27     </H3>
28     <H4>
29       <xsl:text>商品价格:</xsl:text>
30       <xsl:value-of select="$p"/>
31     </H4>
32     <H4>
33       <xsl:text>商品数量:</xsl:text>
34       <xsl:value-of select="$q"/>
35     </H4>
36     <H4>
37       <xsl:text>商品总价:</xsl:text>
38       <xsl:value-of select="$p*$q"/>
39     </H4>
40   </xsl:template>
template调用方式实现

 

 

8、深层次实例

XML文件代码:

 1 <roster>
 2     <student ID="101">
 3         <name>李华</name>
 4         <sex></sex>
 5         <birthday>1978.9.12</birthday>
 6         <score>92</score>
 7         <skill>Java</skill>
 8         <skill>Oracle</skill>
 9         <skill>C Sharp</skill>
10         <skill>SQL Server</skill>
11     </student>
12     <student ID="102">
13         <name>倪冰</name>
14         <sex></sex>
15         <birthday>1979.1.12</birthday>
16         <score>89</score>
17         <skill>Visual Basic</skill>
18         <skill>SQL Server</skill>
19         <skill>ASP</skill>
20     </student>
21     <student ID="103">
22         <name>张君宝</name>
23         <sex></sex>
24         <birthday>1982.9.9</birthday>
25         <score>100</score>
26         <skill>C Sharp</skill>
27         <skill>SQL Server</skill>
28         <skill>UML</skill>
29     </student>
30     <student ID="104">
31         <name>杨惠</name>
32         <sex></sex>
33         <birthday>1980.5.16</birthday>
34         <score>85</score>
35         <skill>Visual C++</skill>
36         <skill>SQL Server</skill>
37         <skill>UML</skill>
38     </student>
39     <student ID="105">
40         <name>崔春晓</name>
41         <sex></sex>
42         <birthday>1981.4.19</birthday>
43         <score>86</score>
44         <skill>UML</skill>
45         <skill>C Sharp</skill>
46         <skill>XML</skill>
47         <skill>SQL Server</skill>
48     </student>
49     <student ID="106">
50         <name>王动</name>
51         <sex></sex>
52         <birthday>1981.4.19</birthday>
53         <score>59</score>
54         <skill>UML</skill>
55         <skill>C Sharp</skill>
56         <skill>XML</skill>
57         <skill>SQL Server</skill>
58     </student>    
59     <student ID="107">
60         <name>李寻欢</name>
61         <sex></sex>
62         <birthday>1981.4.19</birthday>
63         <score>58</score>
64         <skill>UML</skill>
65         <skill>C Sharp</skill>
66         <skill>XML</skill>
67         <skill>SQL Server</skill>
68     </student>    
69 </roster>
xml代码

 

实现效果图:

实现要点简述:1、偶数行背景为绿色

                       2、按照成绩排序

                       3、女生的名字蓝色显示

                       4、评价的判判断

                       5、总分、总人数、平均分(保留一位小数)的计算

 

XSLT代码:

  1   <xsl:template match="/roster">
  2     <title>学生成绩单</title>
  3     <table border="1"  cellspacing="0" align="centre">
  4       <tr align="centre" bgcolor="#dadada">
  5         <td>编号</td>
  6         <td>姓名</td>
  7         <td>性别</td>
  8         <td>生日</td>
  9         <td>成绩</td>
 10         <td>评价</td>
 11       </tr>
 12       
 13       <xsl:for-each select="student">
 14         <!--对 这 for-each中的结果进行 排序-->
 15         <xsl:sort select="score" order="descending" data-type="number"/>
 16         
 17         <tr>
 18           <!--如果是偶数行的话,背景颜色 就是 绿色-->
 19           <xsl:if test="position() mod 2 =0">
 20             <!--为上级元素(tr) 添加属性-->
 21             <xsl:attribute name="bgcolor">green</xsl:attribute>
 22           </xsl:if>
 23 
 24           <td>
 25             <xsl:value-of select="@ID"/> <!--取 xml 元素中的属性  使用@ 符号来取得-->
 26           </td>
 27           <td>
 28             <font>
 29               <xsl:if test="sex = '女'">
 30                 <xsl:attribute name="color">bule</xsl:attribute>
 31               </xsl:if>
 32               <xsl:value-of select="name"/>
 33             </font>
 34           </td>
 35           <td>
 36             <xsl:value-of select="sex"/>
 37           </td>
 38           <td>
 39             <xsl:value-of select="birthday"/>
 40           </td>
 41           <td>
 42             <xsl:value-of select="score"/>
 43           </td>
 44           <td>
 45             <!--相当好case的实现-->
 46             <xsl:choose>
 47                           <!--在xslt中 大于和小于号  是特殊字符  所有要用 &lt;和&gt;来代替-->
 48               <xsl:when test="score &lt; 60">不及格</xsl:when>
 49               <xsl:when test="socre &lt; 80">一般</xsl:when>
 50               <xsl:when test="score &gt; 90">优秀</xsl:when>
 51               <xsl:otherwise>良好</xsl:otherwise>
 52             </xsl:choose>
 53           </td>
 54         </tr>
 55 
 56         
 57       </xsl:for-each>
 58       <!--求出 总分和总人数 ,定义成两个变量,,为了下面 计算 平均分使用-->
 59       <xsl:variable name="allscore" select="sum(/roster/student/score)"></xsl:variable>
 60       <xsl:variable name="allstudent" select="count(/roster/student)"></xsl:variable>
 61       <tr>
 62         <td colspan="5" align="center">
 63           <xsl:text>总成绩</xsl:text>
 64         </td>
 65         <td>
 66           <xsl:value-of select="$allscore"/>
 67         </td>
 68       </tr>
 69 
 70       <tr>
 71         <td colspan="5" align="center">
 72           <xsl:text>总人数</xsl:text>
 73         </td>
 74         <td>
 75           <xsl:value-of select="$allstudent"/>
 76         </td>
 77       </tr>
 78 
 79       <tr>
 80         <td colspan="5" align="center">
 81           <xsl:text>平均分</xsl:text>
 82         </td>
 83         <td>
 84           <xsl:value-of select="round($allscore div $allstudent * 10) div 10"/><!--round() 取整函数(四舍五入)-->
 85           <br></br>
 86           <!--第二种  使用 格式化的 方式-->
 87           <xsl:value-of select="format-number($allscore div $allstudent,'##,###.0')"></xsl:value-of><!--#:可以有数字也可以无数字;,:千位分隔符;;0:如果有数字就显示数字,没有就用0代替。 这个格式化:结尾保留一位小数-->
 88         </td>
 89       </tr>
 90     </table>
 91     <hr></hr>
 92     <div>
 93       <xsl:text>60分一下共:</xsl:text>
 94       <strong>
 95                              <!--在 求值函数 中 进行筛选 在对象后面加上 [] 中括号中就是筛选内容-->
 96         <xsl:value-of select="count(/roster/student[score &lt; 60])"/>
 97       </strong>
 98      
 99       <xsl:text></xsl:text>
100       <br></br>
101 
102       <xsl:text>85(包含)以上的女生共有</xsl:text>
103       <xsl:value-of select="count(/roster/student[sex='女' and (score &gt;85 or score=85)])"/>
104       <xsl:text></xsl:text>
105     </div>
106   </xsl:template>
107 
108   <!--match()匹配可以用 ‘|’来匹配多个模式-->
109   <xsl:template name="a" match="a">
110     <!--根据名字调用一个模板-->
111     <xsl:call-template name="a">
112       
113     </xsl:call-template>
114   </xsl:template>
XSLT代码

 

 

9、总结

 1》转换引擎也称为 XSLT 处理器

 2》将 XML 文档和 XSL 样式表作为输入提供给 XSLT 处理器

 3》XSLT处理器 是一个可以将 XML 文档转换为其他 XML 文档或其他格式的 XML 应用程序

 4》模板规则定义将 XML 元素节点转换为 XSL 元素节点的方法

 5》XSLT 使用 XPath 标识匹配节点

 6》可以使用 XSL 元素创建 XSL 样式表

 

posted @ 2014-03-15 14:42  小小gogo  Views(2643)  Comments(0Edit  收藏  举报