使用 XSLT 将 XML 转换为 XHTML

1. 什么是XSLT
大家可能听说过XSL(eXtensible Stylesheet Language),XSL和我们这里说的XSLT从狭义上理解是一样的,而按照W3C的标准,XSLT的说法更严格些,因此我们在文章中统一使用 XSLT的称法。XSLT的英文标准名称为eXtensible Stylesheet Language Transformation。根据W3C的规范说明书(http://www.w3.org/TR/xslt),最早设计XSLT的用意是帮助XML文档(document)转换为其它文档。但是随着发展,XSLT已不仅仅用于将XML转换为HTML或其它文本格式,更全面的定义应该是: XSLT是一种用来转换XML文档结构的语言。

2. 为什么要用XSLT
我们已经知道,XML是一种电脑程序间交换原始数据的简单而标准的方法。它的成功并不在于它容易被人们书写和阅读,更重要的是,它从根本上解决了应用系统间的信息交换。因为XML满足了两个基本的需求:
(1).将数据和表达形式分离。就象天气预报的信息可以显示在不同的设备上,电视,手机或者其它。
(2).在不同的应用之间传输数据。电子商务数据交换的与日俱增使得这种需求越来越紧迫。
为了使数据便于人们的阅读理解,我们需要将信息显示出来或者打印出来,例如将数据变成一个HTML文件,一个PDF文件,甚至是一段声音;同样,为了使数据适合不同的应用程序,我们必须有能够将一种数据格式转换为另一种数据格式,比如需求格式可能是一个文本文件,一个SQL语句,一个HTTP信息,一定顺序的数据调用等。而XSLT就是我们用来实现这种转换功能的语言。将XML转换为HTML,是目前XSLT最主要的功能。

3.如何使用XSLT

我们首先需要创建一个xsl文档并在文档顶端声明XSLT命名空间,根据 W3C 的 XSLT 标准,声明 XSL 样式表的正确方法是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

或者:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

这两种声明方式意义完全相同,不知道为什么W3C会使用两种方式声明。完整的xsl文档内容如下:

cdcatalog.xsl
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 <body>
6 <h2>My CD Collection</h2>
7 <table border="1">
8 <tr bgcolor="#9acd32">
9 <th align="left">Title</th>
10 <th align="left">Artist</th>
11 <th align="left">Price</th>
12 </tr>
13 <xsl:for-each select="catalog/cd">
14 <tr>
15 <td>
16 <xsl:value-of select="title"/>
17 </td>
18 <td>
19 <xsl:value-of select="artist"/>
20 </td>
21 <td>
22 <xsl:value-of select="price"/>
23 </td>
24 </tr>
25 </xsl:for-each>
26 </table>
27 </body>
28 </html>
29 </xsl:template>
30  </xsl:stylesheet>
31  

接下来,我们需要再创建一个保存数据的xml文档:

cdcatalog.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2  <catalog>
3 <cd>
4 <title>Empire Burlesque</title>
5 <artist>Bob Dylan</artist>
6 <country>USA</country>
7 <company>Columbia</company>
8 <price>10.90</price>
9 <year>1985</year>
10 </cd>
11 <cd>
12 <title>Hide your heart</title>
13 <artist>Bonnie Tyler</artist>
14 <country>UK</country>
15 <company>CBS Records</company>
16 <price>9.90</price>
17 <year>1988</year>
18 </cd>
19 <cd>
20 <title>Greatest Hits</title>
21 <artist>Dolly Parton</artist>
22 <country>USA</country>
23 <company>RCA</company>
24 <price>9.90</price>
25 <year>1982</year>
26 </cd>
27 <cd>
28 <title>Still got the blues</title>
29 <artist>Gary Moore</artist>
30 <country>UK</country>
31 <company>Virgin records</company>
32 <price>10.20</price>
33 <year>1990</year>
34 </cd>
35 <cd>
36 <title>Eros</title>
37 <artist>Eros Ramazzotti</artist>
38 <country>EU</country>
39 <company>BMG</company>
40 <price>9.90</price>
41 <year>1997</year>
42 </cd>
43 <cd>
44 <title>One night only</title>
45 <artist>Bee Gees</artist>
46 <country>UK</country>
47 <company>Polydor</company>
48 <price>10.90</price>
49 <year>1998</year>
50 </cd>
51 <cd>
52 <title>Sylvias Mother</title>
53 <artist>Dr.Hook</artist>
54 <country>UK</country>
55 <company>CBS</company>
56 <price>8.10</price>
57 <year>1973</year>
58 </cd>
59 <cd>
60 <title>Maggie May</title>
61 <artist>Rod Stewart</artist>
62 <country>UK</country>
63 <company>Pickwick</company>
64 <price>8.50</price>
65 <year>1990</year>
66 </cd>
67 <cd>
68 <title>Romanza</title>
69 <artist>Andrea Bocelli</artist>
70 <country>EU</country>
71 <company>Polydor</company>
72 <price>10.80</price>
73 <year>1996</year>
74 </cd>
75 <cd>
76 <title>When a man loves a woman</title>
77 <artist>Percy Sledge</artist>
78 <country>USA</country>
79 <company>Atlantic</company>
80 <price>8.70</price>
81 <year>1987</year>
82 </cd>
83 <cd>
84 <title>Black angel</title>
85 <artist>Savage Rose</artist>
86 <country>EU</country>
87 <company>Mega</company>
88 <price>10.90</price>
89 <year>1995</year>
90 </cd>
91 <cd>
92 <title>1999 Grammy Nominees</title>
93 <artist>Many</artist>
94 <country>USA</country>
95 <company>Grammy</company>
96 <price>10.20</price>
97 <year>1999</year>
98 </cd>
99 <cd>
100 <title>For the good times</title>
101 <artist>Kenny Rogers</artist>
102 <country>UK</country>
103 <company>Mucik Master</company>
104 <price>8.70</price>
105 <year>1995</year>
106 </cd>
107 <cd>
108 <title>Big Willie style</title>
109 <artist>Will Smith</artist>
110 <country>USA</country>
111 <company>Columbia</company>
112 <price>9.90</price>
113 <year>1997</year>
114 </cd>
115 <cd>
116 <title>Tupelo Honey</title>
117 <artist>Van Morrison</artist>
118 <country>UK</country>
119 <company>Polydor</company>
120 <price>8.20</price>
121 <year>1971</year>
122 </cd>
123 <cd>
124 <title>Soulsville</title>
125 <artist>Jorn Hoel</artist>
126 <country>Norway</country>
127 <company>WEA</company>
128 <price>7.90</price>
129 <year>1996</year>
130 </cd>
131 <cd>
132 <title>The very best of</title>
133 <artist>Cat Stevens</artist>
134 <country>UK</country>
135 <company>Island</company>
136 <price>8.90</price>
137 <year>1990</year>
138 </cd>
139 <cd>
140 <title>Stop</title>
141 <artist>Sam Brown</artist>
142 <country>UK</country>
143 <company>A and M</company>
144 <price>8.90</price>
145 <year>1988</year>
146 </cd>
147 <cd>
148 <title>Bridge of Spies</title>
149 <artist>T`Pau</artist>
150 <country>UK</country>
151 <company>Siren</company>
152 <price>7.90</price>
153 <year>1987</year>
154 </cd>
155 <cd>
156 <title>Private Dancer</title>
157 <artist>Tina Turner</artist>
158 <country>UK</country>
159 <company>Capitol</company>
160 <price>8.90</price>
161 <year>1983</year>
162 </cd>
163 <cd>
164 <title>Midt om natten</title>
165 <artist>Kim Larsen</artist>
166 <country>EU</country>
167 <company>Medley</company>
168 <price>7.80</price>
169 <year>1983</year>
170 </cd>
171 <cd>
172 <title>Pavarotti Gala Concert</title>
173 <artist>Luciano Pavarotti</artist>
174 <country>UK</country>
175 <company>DECCA</company>
176 <price>9.90</price>
177 <year>1991</year>
178 </cd>
179 <cd>
180 <title>The dock of the bay</title>
181 <artist>Otis Redding</artist>
182 <country>USA</country>
183 <company>Atlantic</company>
184 <price>7.90</price>
185 <year>1987</year>
186 </cd>
187 <cd>
188 <title>Picture book</title>
189 <artist>Simply Red</artist>
190 <country>EU</country>
191 <company>Elektra</company>
192 <price>7.20</price>
193 <year>1985</year>
194 </cd>
195 <cd>
196 <title>Red</title>
197 <artist>The Communards</artist>
198 <country>UK</country>
199 <company>London</company>
200 <price>7.80</price>
201 <year>1987</year>
202 </cd>
203 <cd>
204 <title>Unchain my heart</title>
205 <artist>Joe Cocker</artist>
206 <country>USA</country>
207 <company>EMI</company>
208 <price>8.20</price>
209 <year>1987</year>
210 </cd>
211  </catalog>

 最后一步,为了使用xsl样式表把xml转换为XHTML,我们将xsl样式表引用添加到xml文档中:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
3  <catalog>
4 <cd>
5 <title>Empire Burlesque</title>
6 <artist>Bob Dylan</artist>
7 <country>USA</country>
8 <company>Columbia</company>
9 <price>10.90</price>
10 <year>1985</year>
11 </cd>
12 ...
13  </catalog>

好了,现在我们用浏览器打开cdcatalog.xml文档,可以发现xml格式的数据变为表格了,就像我们用html语言编写的也没一样,很神奇吧!

 

参考资料:

1.百度知道xslt是什么文件

2.百度百科XSLT

3.W3cSchool的XSLT基础

posted @ 2010-03-31 23:58  JStar  阅读(1107)  评论(0编辑  收藏  举报