從一個原始的 XML(cdcatalog.xml)文檔開始
| <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <uri>www.akmumu.com</uri> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> . . . </catalog> |
這時候使用瀏覽器訪問xml文件,會以各瀏覽器的標準xml樣式展示,有的時候我們需要以自己定義的樣式預覽,比如定義一個a標籤,裏面有xml的鏈接,並可以點擊,這時候我們需要建立一個XSL(cdcatalog.xsl) 樣式表文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?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>
<body>
<h2>My
CD Collection</h2>
<table
border="1">
<tr
bgcolor="#9acd32">
<th
align="left">title</th>
<th
align="left">url</th>
</tr>
<xsl:for-each
select="catalog/cd">
<tr>
<td><xsl:value-of
select="title"/></td>
<td>
<xsl:element
name="a">
<xsl:attribute
name="href"><xsl:value-of
select=“uri”></xsl:value-of></xsl:attribute>
<xsl:attribute
name="target">_blank</xsl:attribute>
<xsl:value-of
select="uri"></xsl:value-of>
</xsl:element>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
這時候我們只需要向原xml文件增加一行
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml
version="1.0"
encoding="ISO-8859-1"?>
<?xml-stylesheet
type="text/xsl"
href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire
Burlesque</title>
<uri>www.akmumu.com</uri>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
.
</catalog>
|
再從瀏覽器訪問xml文件,樣式出現了,a鏈接可以直接點擊。完畢!