《XML案例教程》課件第6章_第1頁
《XML案例教程》課件第6章_第2頁
《XML案例教程》課件第6章_第3頁
《XML案例教程》課件第6章_第4頁
《XML案例教程》課件第6章_第5頁
已閱讀5頁,還剩148頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第6章XSLT模板轉(zhuǎn)換6.1XSLT概述6.2XSLT基本結(jié)構(gòu)6.3XSLT模板規(guī)則6.4常用輸出指令6.5節(jié)點(diǎn)選擇與路徑匹配6.6流程控制6.7元素排序6.8綜合實(shí)例6.9本章小結(jié)習(xí)題6

6.1XSLT概述

XML文檔只能表現(xiàn)數(shù)據(jù)的內(nèi)容,而實(shí)際應(yīng)用中數(shù)據(jù)往往表現(xiàn)為形式。CSS是表現(xiàn)數(shù)據(jù)形式的一種樣式語言,在第5章中進(jìn)行了詳細(xì)討論,本章將介紹另外一種表現(xiàn)XML文檔的樣式語言XSL(ExtensibleStylesheetLanguage)。

XSL包含兩部分內(nèi)容:XSLT(XSLTransformation)和FO(FormatObject)。XSLT可以將XML文檔轉(zhuǎn)換為其他文檔形式,如XML、HTML、TEXT等。FO用于格式化輸出,W3C對(duì)FO還未形成統(tǒng)一標(biāo)準(zhǔn),本章將不予討論。本章主要介紹如何使用XSLT將XML文檔轉(zhuǎn)換為其他XML文檔、HTML和TEXT文檔。

6.2XSLT基本結(jié)構(gòu)

本節(jié)通過一個(gè)案例來介紹XSLT的基本結(jié)構(gòu)。例6-1中的ch6-1.xml文檔保存了一本圖書的基本信息,ch6-1.xsl文檔為外部樣式文檔可以輸出ch6-1.xml的數(shù)據(jù)?!纠?-1】

案例說明:引入外部XSL樣式文件輸出XML文檔的數(shù)據(jù)。

程序名稱:ch6-1.xml

01<?xmlversion="1.0"encoding="UTF-8"?>

02<?xml-stylesheettype="text/xsl"href="ch6-1.xsl"?>

03<Book>

04<Title>軟件工程</Title>

05<Author>鄧良松,劉海巖,陸麗娜</Author>06<Publisher>西安電子科技大學(xué)出版社</Publisher>

07<PubDate>2001年6月</PubDate>

08<ISBN>7-5606-0924-4</ISBN>

09</Book>程序名稱:ch6-1.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<xsl:value-ofselect="*"/>

05</xsl:template>

06</xsl:stylesheet>案例分析:在程序ch6-1.xml中語句2使用xml-stylesheet處理指令引入外部樣式文件。type的屬性值“text/xsl”指明加載的外部文件為XSL文件。href屬性指出加載的外部文件路徑,路徑有三種:絕對(duì)路徑、相對(duì)路徑和Web路徑。絕對(duì)路徑指外部XSL文件存放在本地某個(gè)文件夾中,一般該文件與XML文檔不在同一個(gè)文件夾中,引用時(shí)需寫出文件的本地完整路徑,如“C:\Test\ch6-1.xsl”。相對(duì)路徑指外部XSL文件與XML文件在相同的文件夾中,引用時(shí)只需寫出XSL文件名稱即可,如程序ch6-1.xml中第2行對(duì)“ch6-1.xsl”的引用。Web路徑指外部XSL文件位于遠(yuǎn)程Web服務(wù)器上,引用時(shí)需要寫出完整的服務(wù)器域名地址和文件目錄路徑,如“3/xsl/ch6-1.xsl”。6.2.1XML文檔節(jié)點(diǎn)樹

使用XSL轉(zhuǎn)換XML文檔,一個(gè)完整的XML文檔可以轉(zhuǎn)換為一棵結(jié)構(gòu)完整的節(jié)點(diǎn)樹。圖6-1所示是例6-1中程序ch6-1.xml文檔對(duì)應(yīng)的節(jié)點(diǎn)樹。節(jié)點(diǎn)樹中的根節(jié)點(diǎn)在XSL中用“/”表示,而不是XML文檔的根元素Book。XML文檔的根元素只能作為節(jié)點(diǎn)樹中根節(jié)點(diǎn)的一個(gè)子節(jié)點(diǎn)。程序ch6-1.xml中第2行引入外部樣式文件的處理指令也是根節(jié)點(diǎn)的一個(gè)子節(jié)點(diǎn)。圖6-1例6-1中ch6-1.xml文檔轉(zhuǎn)換后的節(jié)點(diǎn)樹

XML文檔中,元素、元素屬性、元素文本內(nèi)容、注釋和處理指令都是節(jié)點(diǎn)樹中的一個(gè)節(jié)點(diǎn)。搜索XML中的數(shù)據(jù)時(shí),從節(jié)點(diǎn)樹的根節(jié)點(diǎn)開始搜索。節(jié)點(diǎn)樹為XSL訪問XML文檔提供了高效、有序的組織結(jié)構(gòu)。6.2.2XSL樣式單結(jié)構(gòu)

例6-1中程序ch6-1.xsl描述了XSL樣式單的基本結(jié)構(gòu)。XSL文檔本質(zhì)上是XML文檔,任何XSL文檔中的第一行語句必須是XML的聲明語句,如程序ch6-1.xsl中的第1行為XML的聲明語句,表明該樣式文檔為XML文檔,樣式文檔中標(biāo)記的使用必須符合XML規(guī)范。程序ch6-1.xsl第2行中的xmlns:xsl=“/1999/XSL/Transform"聲明了一個(gè)命名空間,并與字符串xsl綁定,該命名空間是W3C在1999年11月16日發(fā)布的XSL轉(zhuǎn)換語言標(biāo)準(zhǔn)。XSL文檔的根元素為<xsl:stylesheet>,它是命名空間xsl中的一個(gè)元素。版本號(hào)version="1.0"是元素<xsl:stylesheet>的一個(gè)屬性。第3行至第5行中的元素和屬性都屬于xsl命名空間。具體應(yīng)用在下節(jié)中介紹。

6.3XSLT模板規(guī)則

模板規(guī)則是XSL轉(zhuǎn)換中最重要的部分。模板轉(zhuǎn)化需要使用模板定義元素</xsl:template>和模板調(diào)用元素</xsl:apply-templates>,本節(jié)將討論模板定義語法和模板調(diào)用規(guī)則。【例6-2】

案例說明:使用模板規(guī)則將XML文檔轉(zhuǎn)換為HTML文檔。

程序名稱:ch6-2.xml

01<?xmlversion="1.0"encoding="UTF-8"?>

02<?xml-stylesheettype="text/xsl"href="ch6-2.xsl"?>

03<BookList>

04<Book>

05<Title>軟件系統(tǒng)開發(fā)技術(shù)(修訂版)</Title>

06<Author>潘錦平,施小英,姚天昉</Author>

07<Publisher>西安電子科技大學(xué)出版社</Publisher>

08<PubDate>2001年1月</PubDate>09<ISBN>7-5606-0492-7</ISBN>

10</Book>

11<Book>

12<Title>軟件工程</Title>

13<Author>鄧良松,劉海巖,陸麗娜</Author>

14<Publisher>西安電子科技大學(xué)出版社</Publisher>

15<PubDate>2001年6月</PubDate>

16<ISBN>7-5606-0924-4</ISBN>

17</Book>

18</BookList>程序名稱:ch6-2.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<html>

05<head>

06<title>圖書信息簡(jiǎn)介</title>

07</head>

08<body>09<xsl:apply-templatesselect="BookList"/>

10</body>

11</html>

12</xsl:template>

13<xsl:templatematch="BookList">

14<tablewidth="650"align="center"cellpadding="1"cellspacing="1">

15<trbgcolor="#c0c060">

16<th>書名</th>

17<th>作者</th>

18<th>出版社</th>19<th>出版日期</th>

20<th>ISBN</th>

21</tr>

22<xsl:apply-templatesselect="Book"/>

23</table>

24</xsl:template>

25<xsl:templatematch="Book">

26<trbgcolor="#ffffc0">

27<td><xsl:value-ofselect="Title"/></td>

28<td><xsl:value-ofselect="Author"/></td>

29<td><xsl:value-ofselect="Publisher"/></td>30<td><xsl:value-ofselect="PubDate"/></td>

31<td><xsl:value-ofselect="ISBN"/></td>

32</tr>

33</xsl:template>

34</xsl:stylesheet>6.3.1模板定義元素和調(diào)用元素

例6-2中的程序ch6-2.xsl文件將程序ch6-2.xml文檔轉(zhuǎn)換為HTML文檔。元素</xsl:template>定義輸入模板,屬性match指定將此模板規(guī)則應(yīng)用于輸入文檔的哪個(gè)節(jié)點(diǎn),如ch6-2中的第3行將模板規(guī)則應(yīng)用于輸入文檔的根節(jié)點(diǎn),第13行將模板規(guī)則應(yīng)用于輸入文檔的根元素BookList,第25行將模板應(yīng)用于子元素Book。

調(diào)用元素</xsl:apply-templates>通常放在輸出模板中,屬性select指定要處理的節(jié)點(diǎn)集。處理時(shí),會(huì)將節(jié)點(diǎn)集中節(jié)點(diǎn)依次與樣式單中的模板進(jìn)行比較,與節(jié)點(diǎn)匹配的模板輸出將被放在該元素所在位置。6.3.2模板轉(zhuǎn)換過程

例6-2中的ch6-2.xsl將ch6-2.xml轉(zhuǎn)換為HTML文檔的過程如下:

(1)程序ch6-2.xsl第03行至第08行將結(jié)果樹的根節(jié)點(diǎn)“/”與樣式單中的模板進(jìn)行匹配,首先輸出如下內(nèi)容:

<html>

<head>

<title>圖書信息簡(jiǎn)介</title>

</head>

<body>

(2)程序ch6-2.xsl第09行使用<xsl:apply-templatesselect="Booklists"/>處理XML文檔的根元素節(jié)點(diǎn)Booklists,將BookList節(jié)點(diǎn)與樣式表中第13行的模板<xsl:templatematch="BookList">進(jìn)行匹配,輸出第14行至第21行的內(nèi)容:

<tablewidth="650"align="center"cellpadding="1"cellspacing="1">

<trbgcolor="#c0c060">

<th>書名</th>

<th>作者</th>

<th>出版社</th>

<th>出版日期</th>

<th>ISBN</th>

</tr>

(3)程序ch6-2.xsl第22行的<xsl:apply-templatesselect="Book"/>對(duì)當(dāng)前節(jié)點(diǎn)下所有Book元素進(jìn)行處理,將所有Book元素節(jié)點(diǎn)與樣式表中第25行的模板<xsl:templatematch="Book">進(jìn)行匹配。在第26行至第32行的模板輸出代碼中使用指令<xsl:value-of>將Book的子元素Title、Author、Publisher、PubDate和ISBN內(nèi)容輸出到表格的單元格中。<xsl:value-of>指令的select屬性用于選擇被提取的節(jié)點(diǎn)。如程序ch6-2.xsl第27行<td><xsl:value-ofselect="Title"/></td>將XML文檔ch6-2.xml第05行和第12行的元素Title的內(nèi)容“軟件系統(tǒng)開發(fā)技術(shù)(修訂版)”和“軟件工程”依次提取到表格的單元格中。程序ch6-2.xsl第25行至第33行模板執(zhí)行后輸出如下的內(nèi)容:<trbgcolor="#ffffc0">

<td>軟件系統(tǒng)開發(fā)技術(shù)(修訂版)</td>

<td>潘錦平,施小英,姚天昉</td>

<td>西安電子科技大學(xué)出版社</td>

<td>2001年1月</td>

<td>7-5606-0492-7</td>

</tr>

<trbgcolor="#ffffc0">

<td>軟件工程</td>

<td>鄧良松,劉海巖,陸麗娜</td>

<td>西安電子科技大學(xué)出版社</td>

<td>2001年6月</td>

<td>7-5606-0924-4</td>

</tr>

(4)當(dāng)XML文檔ch6-1.xml中所有Book元素被處理完畢后,程序ch6-2.xsl執(zhí)行第23行的模板輸出剩余標(biāo)記</table>。

(5)程序ch6-2.xsl執(zhí)行到第24行</xsl:template>,第一個(gè)模板<xsl:apply-templatesselect="BookList"/>執(zhí)行完畢。繼續(xù)執(zhí)行第10行至第12行輸出模板<xsl:templatematch="/">中剩余標(biāo)記</body>和</html>。例6-2中的程序處理完畢后輸出的HTML文檔如下:

<html>

<head>

<title>圖書信息簡(jiǎn)介</title>

</head>

<body>

<tablewidth="650"align="center"cellpadding="1"cellspacing="1">

<trbgcolor="#c0c060">

<th>書名</th>

<th>作者</th>

<th>出版社</th>

<th>出版日期</th>

<th>ISBN</th>

</tr>

<trbgcolor="#ffffc0">

<td>軟件系統(tǒng)開發(fā)技術(shù)(修訂版)</td>

<td>潘錦平,施小英,姚天昉</td>

<td>西安電子科技大學(xué)出版社</td>

<td>2001年1月</td>

<td>7-5606-0492-7</td>

</tr>

<trbgcolor="#ffffc0">

<td>軟件工程</td>

<td>鄧良松,劉海巖,陸麗娜</td>

<td>西安電子科技大學(xué)出版社</td>

<td>2001年6月</td>

<td>7-5606-0924-4</td>

</tr>

</table>

</body>

</html>輸出的HTML文檔在瀏覽器中的運(yùn)行結(jié)果如圖6-2

所示。圖6-2例6-2輸出的HTML文檔在瀏覽器中的運(yùn)行結(jié)果

6.4常用輸出指令

上節(jié)介紹了XSLT的模板規(guī)則,每一模板規(guī)則都定義了一組處理指令和規(guī)則。本節(jié)將著重介紹常用輸出指令。6.4.1輸出文本

輸出文本所用到的指令為<xsl:value-of>,主要應(yīng)用就是輸出元素文本內(nèi)容。其語法如下:

<xsl:value-ofselect=“expression”disable-output-escaping=“yes|no”/>

其屬性取值見表6-1。表6-1輸出文本屬性取值在上節(jié)的例6-2中程序ch6-2.xsl第27行至第31行已經(jīng)對(duì)<xsl:value-of>指令的使用作了分析,這里不再重復(fù)。6.4.2生成元素

指令<xsl:element>可用于動(dòng)態(tài)生成元素,生成的元素被直接輸出到目標(biāo)文檔中。其語法如下:

<xsl:elementname=“name”namespace=“URI”use-attribute-sets=“attnamelist”>

<!--Content:template-->

</xsl:element>

其屬性取值見表6-2。表6-2生成元素屬性取值【例6-3】

案例說明:使用<xsl:element>指令生成元素。

程序名稱:ch6-3.xml

01<?xmlversion="1.0"encoding="UTF-8"?>

02<?xml-stylesheettype="text/xsl"href="ch6-3.xsl"?>

03<BookList>

04<Book>

05<Title>軟件系統(tǒng)開發(fā)技術(shù)(修訂版)</Title>

06<Author>潘錦平,施小英,姚天昉</Author>

07<Publisher>西安電子科技大學(xué)出版社</Publisher>08<PubDate>2001年1月</PubDate>

09<ISBN>7-5606-0492-7</ISBN>

10</Book>

11<Book>

12<Title>軟件工程</Title>

13<Author>鄧良松,劉海巖,陸麗娜</Author>

14<Publisher>西安電子科技大學(xué)出版社</Publisher>

15<PubDate>2001年6月</PubDate>

16<ISBN>7-5606-0924-4</ISBN>

17</Book>

18</BookList>程序名稱:ch6-3.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<html>

05<head>

06<title>圖書信息簡(jiǎn)介</title>

07</head>

08<body>09<xsl:apply-templatesselect="BookList"/>

10</body>

11</html>

12</xsl:template>

13<xsl:templatematch="BookList">

14<tablewidth="650"align="center"cellpadding="1"cellspacing="1">

15<trbgcolor="#c0c060">

16<th>編號(hào)</th>

17<th>書名</th>

18<th>作者</th>

19<th>出版社</th>20<th>出版日期</th>

21<th>ISBN</th>

22</tr>

23<xsl:apply-templatesselect="Book"/>

24</table>

25</xsl:template>

26<xsl:templatematch="Book">

27<trbgcolor="#ffffc0">

28<td>

29<xsl:elementname="a">30<xsl:value-ofselect="position()"/>

31</xsl:element>

32</td>

33<td><xsl:value-ofselect="Title"/></td>

34<td><xsl:value-ofselect="Author"/></td>

35<td><xsl:value-ofselect="Publisher"/></td>

36<td><xsl:value-ofselect="PubDate"/></td>

37<td><xsl:value-ofselect="ISBN"/></td>

38</tr>

39</xsl:template>

40</xsl:stylesheet>案例分析:例6-3中的程序ch6-3.xsl第29行至第31行在表格的第一個(gè)單元格生成一個(gè)元素a,元素a的取值為當(dāng)前Book元素的編號(hào)。函數(shù)position()取當(dāng)前元素的編號(hào)。XML文檔ch6-3.xml被轉(zhuǎn)換為HTML文檔后在瀏覽器中的運(yùn)行結(jié)果如圖6-3所示。圖6-3例6-3中程序動(dòng)態(tài)產(chǎn)生元素編號(hào)后在瀏覽器中的運(yùn)行結(jié)果6.4.3生成屬性

指令<xsl:attribute>用于創(chuàng)建屬性,并將屬性添加到元素節(jié)點(diǎn)中。其語法如下:

<xsl:attributename=“attributename”namespace=“uri”>

<!--Content:template-->

</xsl:attribute>

其屬性取值見表6-3。表6-3生成屬性取值

【例6-4】

案例說明:使用<xsl:attribute>指令生成屬性。程序ch6-4.xsl將例6-3中XML文檔ch6-3.xml轉(zhuǎn)換成HTML文檔。程序名稱:ch6-4.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<html>

05<head>

06<title>圖書信息簡(jiǎn)介</title>

07</head>

08<body>09<xsl:apply-templatesselect="BookList"/>

10</body>

11</html>

12</xsl:template>

13<xsl:templatematch="BookList">

14<tablewidth="650"align="center"cellpadding="1"cellspacing="1">

15<trbgcolor="#c0c060">

16<th>編號(hào)</th>

17<th>書名</th>

18<th>作者</th>19<th>出版社</th>

20<th>出版日期</th>

21<th>ISBN</th>

22</tr>

23<xsl:apply-templatesselect="Book"/>

24</table>

25</xsl:template>

26<xsl:templatematch="Book">

27<trbgcolor="#ffffc0">

28<td>

29<xsl:elementname="a">30<xsl:attributename="href">

31Book.xml?id=<xsl:value-ofselect="position()"/>

32</xsl:attribute>

33<xsl:value-ofselect="position()"/>

34</xsl:element>

35</td>

36<td><xsl:value-ofselect="Title"/></td>

37<td><xsl:value-ofselect="Author"/></td>38<td><xsl:value-ofselect="Publisher"/></td>

39<td><xsl:value-ofselect="PubDate"/></td>

40<td><xsl:value-ofselect="ISBN"/></td>

41</tr>

42</xsl:template>

43</xsl:stylesheet>案例分析:例6-4中的ch6-4.xsl文檔第29行至第34行首先使用<xsl:element>指令創(chuàng)建一個(gè)元素a,元素a取值為例6-3中ch6-3.xml文檔當(dāng)前Book元素的編號(hào)。然后為元素a創(chuàng)建一個(gè)屬性href,屬性取值為Book.xml加當(dāng)前Book元素的編號(hào)。如當(dāng)前Book元素的編號(hào)為1時(shí),創(chuàng)建的元素結(jié)構(gòu)為<ahref="Book.xml?id=1">1</a>,該元素為超鏈接,顯示在表格的第一個(gè)單元格中,如圖6-4所示。圖6-4程序ch6-4.xsl將XML文檔ch6-3.xml轉(zhuǎn)換成HTML文檔在瀏覽器中的運(yùn)行結(jié)果6.4.4輸出注釋

指令<xsl:comment>用于在結(jié)果樹中創(chuàng)建一個(gè)注釋節(jié)點(diǎn)。其語法如下:

<xsl:comment>

<!--Content:template-->

</xsl:comment>

例如,<xsl:comment>Thisisacomment!</xsl:comment>在結(jié)果樹中創(chuàng)建一個(gè)注釋節(jié)點(diǎn)<!--Thisisacomment-->。

6.5節(jié)點(diǎn)選擇與路徑匹配

本節(jié)介紹路徑匹配和節(jié)點(diǎn)的選擇方式。

6.5.1使用通配符

1.點(diǎn)號(hào)和星號(hào)匹配符

點(diǎn)號(hào)(.)用于提取當(dāng)前元素節(jié)點(diǎn)以及當(dāng)前元素節(jié)點(diǎn)下所有子節(jié)點(diǎn)的值,星號(hào)(*)用于提取任意名稱的元素節(jié)點(diǎn)值。點(diǎn)號(hào)可以匹配指令、注釋和文本節(jié)點(diǎn),而星號(hào)只能匹配文本節(jié)點(diǎn)。星號(hào)只有在根節(jié)點(diǎn)(“/”)的模板中才能匹配所有子節(jié)點(diǎn),而用于匹配非根節(jié)點(diǎn)的子節(jié)點(diǎn)時(shí),只能匹配節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn)。點(diǎn)號(hào)可以用于匹配非根節(jié)點(diǎn)的所有子節(jié)點(diǎn)。

【例6-5】

案例說明:使用點(diǎn)號(hào)提取例6-3中ch6-3.xml文檔中的指令,并分別使用點(diǎn)號(hào)和星號(hào)提取XML文檔ch6-3.xml中BookList元素的子元素內(nèi)容。程序名稱:ch6-5.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<html>

05<head>

06<title>點(diǎn)號(hào)和星號(hào)的用法</title>

07</head>

08<body>

09<xsl:apply-templates/>10</body>

11</html>

12</xsl:template>

13<xsl:templatematch="processing-instruction()">

14<h4>1.使用"."匹配指令:</h4>

15<xsl:value-ofselect="."/>

16</xsl:template>

17<xsl:templatematch="BookList">

18<h4>2.使用"."匹配BookList元素子節(jié)點(diǎn):</h4>

19<xsl:value-ofselect="."/>20<h4>3.使用"*"匹配BookList元素子節(jié)點(diǎn):</h4>

21<xsl:value-ofselect="*"/>

22</xsl:template>

23</xsl:stylesheet>案例分析:第09行對(duì)例6-3中ch6-3.xml文檔對(duì)應(yīng)的結(jié)果樹的根節(jié)點(diǎn)應(yīng)用模板。第13行至第16行匹配根節(jié)點(diǎn)下的指令,processing-instruction()函數(shù)選擇指令,使用“.”取出指令的值。第17行至第21行匹配根元素BookList元素的子元素,使用“.”取出所有子元素Book的內(nèi)容,使用“*”取出第一個(gè)子元素Book的內(nèi)容。程序ch6-5.xsl加載到XML文檔ch6-3上執(zhí)行的結(jié)果如圖6-5所示。圖6-5程序6-5.xsl匹配例6-3中ch6-3.xml文檔節(jié)點(diǎn)在瀏覽器中的顯示結(jié)果說明:XSL中可以使用節(jié)點(diǎn)類型的函數(shù)選擇節(jié)點(diǎn)。除使用processing-instruction()函數(shù)選擇指令外,還可以使用comment()函數(shù)選擇注釋節(jié)點(diǎn),使用text()函數(shù)選擇文本節(jié)點(diǎn),使用node()函數(shù)選擇任意節(jié)點(diǎn)。

2.根節(jié)點(diǎn)匹配符和根元素匹配符

“/”為根節(jié)點(diǎn)匹配符,匹配根節(jié)點(diǎn)的模板一般不可少?!?*”匹配任意的根元素。

例6-5中的第17行<xsl:templatematch="BookList">可以更改為<xsl:templatematch="/*">。

3.當(dāng)前節(jié)點(diǎn)和父節(jié)點(diǎn)匹配符

當(dāng)前節(jié)點(diǎn)用點(diǎn)號(hào)表示,當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)用雙點(diǎn)號(hào)表示?!纠?-6】

案例說明:使用當(dāng)前節(jié)點(diǎn)和父節(jié)點(diǎn)匹配選擇元素。

程序名稱:ch6-6.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<html>

05<head>

06<title>當(dāng)前節(jié)點(diǎn)和當(dāng)前節(jié)點(diǎn)父節(jié)點(diǎn)的使用</title>07</head>

08<body>

09<xsl:apply-templatesselect="BookList/Book/Title"/>

10</body>

11</html>

12</xsl:template>

13<xsl:templatematch="Title">

14<h4>1.當(dāng)前節(jié)點(diǎn)(.)使用:</h4>

15<xsl:value-ofselect="."/>16<h4>2.父節(jié)點(diǎn)(..)使用:</h4>

17<xsl:value-ofselect="../Publisher"/>

18</xsl:template>

19</xsl:stylesheet>案例分析:第15行使用“.”選擇當(dāng)前節(jié)點(diǎn)Title的值。第17行<xsl:value-ofselect="../Publisher"/>選擇當(dāng)前節(jié)點(diǎn)Title的父節(jié)點(diǎn)的子節(jié)點(diǎn)Publisher的值。程序加載到例6-3中的ch6-3.xml文檔上的結(jié)果如圖6-6所示。圖6-6當(dāng)前節(jié)點(diǎn)和父節(jié)點(diǎn)匹配使用6.5.2路徑選擇

1.絕對(duì)路徑和相對(duì)路徑

絕對(duì)路徑指從根節(jié)點(diǎn)到指定節(jié)點(diǎn)的路徑;相對(duì)路徑指從當(dāng)前節(jié)點(diǎn)到指定節(jié)點(diǎn)的路徑。路徑中的目錄使用“/”作為分隔符。

例6-3中ch6-3.xml文檔“Title”節(jié)點(diǎn)的絕對(duì)路徑為/BookList/Book/Title。如果當(dāng)前節(jié)點(diǎn)為BookList,則“Title”節(jié)點(diǎn)的相對(duì)路徑為Book/Title。

2.在select屬性中使用路徑選擇

【例6-7】

案例說明:在select屬性中使用路徑選擇。程序名稱:ch6-7.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<xsl:apply-templatesselect="BookList/Book"/>

05</xsl:template>

06<xsl:templatematch="Book">07<xsl:value-ofselect="Title"/><br/>

08</xsl:template>

09</xsl:stylesheet>

案例分析:第04行在select中使用相對(duì)路徑匹配Book元素。

3.在match屬性中使用路徑選擇

【例6-8】

案例說明:在match屬性中使用路徑選擇查找元素。程序名稱:ch6-8.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<xsl:apply-templates/>

05</xsl:template>

06<xsl:templatematch="/BookList/Book">07<xsl:value-ofselect="Title"/><br/>

08</xsl:template>

09</xsl:stylesheet>

案例分析:第06行在match中使用絕對(duì)路徑匹配Book元素。

4.路徑中使用星號(hào)

在路徑中可以用星號(hào)代替任意元素節(jié)點(diǎn)名稱。星號(hào)可以匹配結(jié)果樹中某一層中的任意元素。

【例6-9】

案例說明:星號(hào)在路徑中的使用。程序名稱:ch6-9.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<xsl:apply-templatesselect="/BookList/*/Title"/>

05</xsl:template>

06<xsl:templatematch="Title">07<xsl:value-ofselect=“.”/><br/>

08</xsl:template>

09</xsl:stylesheet>

案例分析:第04行在ch6-3.xml文檔對(duì)應(yīng)的結(jié)果樹中使用*代替中間層的Book元素。

5.路徑中使用“//”

在路徑中使用“//”可以直接引用任意層的后代節(jié)點(diǎn)。

例6-9中程序ch6-9.xsl的第04行標(biāo)記<xsl:apply-templatesselect="/BookList/*/Title"/>中的“*”可以使用“//”代替。如<xsl:apply-templatesselect="/BookList//Title"/>。6.5.3選擇元素屬性

可使用“@”選擇元素屬性。

【例6-10】

案例說明:使用“@”輸出元素屬性值。程序名稱:ch6-4.xml

01<?xmlversion="1.0"encoding="UTF-8"?>

02<?xml-stylesheettype="text/xsl"href="ch6-9.xsl"?>

03<BookList>

04<BookCategory="計(jì)算機(jī)">

05<Title>軟件系統(tǒng)開發(fā)技術(shù)(修訂版)</Title>

06<Author>潘錦平,施小英,姚天昉</Author>

07<Publisher>西安電子科技大學(xué)出版社</Publisher>

08<PubDate>2001年1月</PubDate>

09<ISBN>7-5606-0492-7</ISBN>10</Book>

11<BookCategory="小說">

12 <Title>基督山伯爵(上下冊(cè))</Title>

13 <Author>大仲馬(法)</Author>

14 <Publisher>上海譯文出版社</Publisher>

15 <PubDate>2001年8月</PubDate>

16 <ISBN>7-5327-2651-7</ISBN>

17 </Book>

18</BookList>程序名稱:ch6-10.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<xsl:apply-templatesselect="BookList/Book"/>

05</xsl:template>

06<xsl:templatematch="Book">

07<ul>08<li>書名:<xsl:value-ofselect=“Title”/><br/>

09類別:<xsl:value-ofselect=“@Category”/>

10</li>

11</ul>

12</xsl:template>

13</xsl:stylesheet>

案例分析:例6-10中程序ch6-4.xml第04行和第11行Book元素聲明屬性Category,程序ch6-10.xsl第09行指令<xsl:value-ofselect="@Category"/>提取Category的值。6.5.4使用條件選擇節(jié)點(diǎn)

可以在元素名稱后使用“[]”為選擇的元素添加限制條件。

如在例6-10中的ch6-4.xml文檔中選擇具有子元素“Publisher”的“Book”元素,并輸出書名,可以使用如下的模板規(guī)則:

<xsl:templatematch=“Book[Publisher]”>

<xsl:value-ofselect=“Title”/>

</xsl:template>在“[]”中使用“|”組合多個(gè)限制條件。如選擇具有“Publisher”或者“ISBN”的元素節(jié)點(diǎn),match的屬性值可以更改為“Book[Publisher|ISBN]”。

在“[]”中使用“@”來指定元素帶有屬性。如選擇具有屬性“Category”的元素,match的屬性值可以更改為“Book[@Category]”。

在“[]”可以使用表達(dá)式選擇節(jié)點(diǎn)。如選擇屬性“Category”為“計(jì)算機(jī)”的Book元素,match的屬性值可以更改為“Book[@Category='計(jì)算機(jī)']”。

6.6流程控制

XSL中的流程控制主要有單條件流程控制、多條件流程控制(多條件選擇)和循環(huán)處理。

6.6.1單條件流程控制

XSL中的單條件流程控制指令為<xsl:if>元素,該指令的使用語法如下:

<xsl:iftest=“expression”><!--Content:template--></xsl:if>

屬性test為條件表達(dá)式。

【例6-11】

案例說明:使用<xsl:if>指令選取例6-10中ch6-4.xml文檔中Category值為“計(jì)算機(jī)”的圖書信息。程序ch6-11.xsl中給出模板規(guī)則。程序名稱:ch6-11.xsl

01<xsl:templatematch="Book">

02<xsl:iftest="@Category='計(jì)算機(jī)'">

03<trbgcolor="#ffffc0">

04<td><xsl:value-ofselect="Title"/></td>

05<td><xsl:value-ofselect="Author"/></td>

06<td><xsl:value-ofselect="Publisher"/></td>

07<td><xsl:value-ofselect="PubDate"/></td>

08<td><xsl:value-ofselect="ISBN"/></td>09<td><xsl:value-ofselect=“@Category”/></td>

10</tr>

11</xsl:if>

12</xsl:template>

案例分析:第02行至第11行使用<xsl:if>指令對(duì)圖書內(nèi)容有條件地輸出,屬性test的值為“@Category='計(jì)算機(jī)'”表示輸出所有計(jì)算機(jī)類圖書。6.6.2多條件選擇

指令<xsl:choose>、<xsl:when>和<xsl:otherwise>擴(kuò)展了<xsl:if>的功能。<xsl:choose>的子元素<xsl:when>用于提供多條件選擇,<xsl:otherwise>提供類似于else的功能。

【例6-12】

案例說明:以不同顏色輸出程序ch6-4.xml中不同類別的圖書。程序ch6-12.xsl中給出模板規(guī)則。程序名稱:ch6-12.xsl

01<xsl:templatematch="Book">

02<xsl:choose>

03<xsl:whentest="@Category='計(jì)算機(jī)'">

04<trbgcolor="#ffffc0">

05<td><xsl:value-ofselect="Title"/></td>

06<td><xsl:value-ofselect="Author"/></td>

07<td><xsl:value-ofselect="Publisher"/></td>

08<td><xsl:value-ofselect="PubDate"/></td>09<td><xsl:value-ofselect="ISBN"/></td>

10<td><xsl:value-ofselect="@Category"/></td>

11</tr>

12</xsl:when>

13<xsl:otherwise>

14<trbgcolor="#ffc0c0">

15<td><xsl:value-ofselect="Title"/></td>

16<td><xsl:value-ofselect="Author"/></td>

17<td><xsl:value-ofselect="Publisher"/></td>

18<td><xsl:value-ofselect="PubDate"/></td>19<td><xsl:value-ofselect="ISBN"/></td>

20<td><xsl:value-ofselect="@Category"/></td>

21</tr>

22</xsl:otherwise>

23</xsl:choose>

24</xsl:template>案例分析:第03行判斷圖書的類別是否為“計(jì)算機(jī)”,如果是“計(jì)算機(jī)”,則以背景色“#ffffc0”輸出圖書內(nèi)容,其中第04行設(shè)置表格行背景色。如果圖書的類別不是“計(jì)算機(jī)”,則以背景色“#ffc0c0”輸出圖書的內(nèi)容,背景色設(shè)置在第14行完成。6.6.3循環(huán)處理

XSL中可以使用<xsl:for-each>元素實(shí)現(xiàn)循環(huán)功能。

【例6-13】

案例名稱:使用指令<xsl:for-each>代替模板規(guī)則實(shí)現(xiàn)從ch6-4.xml文檔中提取數(shù)據(jù)。程序ch6-13.xsl給出使用<xsl:for-each>處理BookList元素和Book元素的代碼片段。程序名稱:ch6-13.xsl

01<xsl:for-eachselect="BookList">

02<tablewidth="650"align="center"cellpadding="1"cellspacing="1">

03<trbgcolor="#c0c060">

04<th>書名</th>

05<th>作者</th>

06<th>出版社</th>

07<th>出版日期</th>

08<th>ISBN</th>

09<th>類別</th>10</tr>

11<xsl:for-eachselect="Book">

12<trbgcolor="#ffffc0">

13<td><xsl:value-ofselect="Title"/></td>

14<td><xsl:value-ofselect="Author"/></td>

15<td><xsl:value-ofselect="Publisher"/></td>

16<td><xsl:value-ofselect="PubDate"/></td>

17<td><xsl:value-ofselect="ISBN"/></td>

18<td><xsl:value-ofselect="@Category"/></td>19</tr>

20</xsl:for-each>

21</table>

22</xsl:for-each>案例分析:第01行的循環(huán)語句處理“BookList”節(jié)點(diǎn),第11行的循環(huán)語句處理“Book”元素。第02行至第10行繪制表頭,第12行至第19行繪制表格數(shù)據(jù)單元格,并將書名、作者等XML元素內(nèi)容填充到表格單元格中。

6.7元素排序

排序可以為XSLT中輸出的記錄建立規(guī)則,使得經(jīng)過XSLT轉(zhuǎn)換后的XML數(shù)據(jù)輸出變得有規(guī)則。XSL中使用<xsl:sort>進(jìn)行排序處理,將若干記錄按一定順序輸出。

【例6-14】

案例名稱:使用<xsl:sort>對(duì)ch6-4.xml文檔內(nèi)容進(jìn)行排序輸出。程序名稱:ch6-14.xsl

01<?xmlversion="1.0"encoding="UTF-8"?>

02<xsl:stylesheetversion="1.0"xmlns:xsl="/1999/XSL/Transform">

03<xsl:templatematch="/">

04<html>

05<head><title>圖書信息簡(jiǎn)介</title></head>

06<body>

07<xsl:apply-templatesselect="BookList"/>

08</body>09</html>

10</xsl:template>

11<xsl:templatematch="BookList">

12<tablewidth="650"align="center"cellpadding="1"cellspacing="1">

13<trbgcolor="#c0c060">

14<th>書名</th>

15<th>作者</th>

16<th>出版社</th>

17<th>出版日期</th>18<th>ISBN</th>

19<th>類別</th>

20</tr>

21<xsl:apply-templatesselect="Book">

22<xsl:sortselect="@Category"order="descending"/>

23</xsl:apply-templates>

24</table>

25</xsl:template>

26<xsl:templatematch="Book">

27<trbgcolor="#ffffc0">28<td><xsl:value-ofselect="Title"/></td>

29<td><xsl:value-ofselect="Author"/></td>

30<td><xsl:value-ofselect="Publisher"/></td>

31<td><xsl:value-ofselect="PubDate"/></td>

32<td><xsl:value-ofselect="ISBN"/></td>

33<td><xsl:value-ofselect="@Category"/></td>

34</tr>

35</xsl:template>

36</xsl:stylesheet>案例分析:例6-14第22行<xsl:sortselect="@Category"order="descending"/>對(duì)Book元素按類別“Category”降序輸出。如果需要升序輸出,則修改order的值為ascending。如果存在一個(gè)以上的<xsl:sort>元素,則輸出內(nèi)容先按第一關(guān)鍵字排序,然后再按第二關(guān)鍵字排序,依次類推。

6.8綜合實(shí)例

本節(jié)綜合使用XSLT技術(shù)編寫一個(gè)圖書信息查詢頁面,查詢頁面分為兩個(gè)功能頁面:圖書簡(jiǎn)要信息顯示頁面和圖書詳細(xì)信息顯示頁面。圖書簡(jiǎn)要信息顯示頁面列表顯示圖書的簡(jiǎn)要內(nèi)容,如書名、作者、出版社、出版時(shí)間、定價(jià)和圖書類別。圖書簡(jiǎn)要信息顯示頁面要提供查詢圖書具體信息的超鏈接。圖書簡(jiǎn)要信息顯示頁面如圖6-7所示。圖6-7圖書簡(jiǎn)要信息列表顯示頁面圖書詳細(xì)信息顯示頁面包含圖書內(nèi)容簡(jiǎn)介和圖書封面,如圖6-8所示。詳細(xì)顯示頁面需要提供一個(gè)“返回”鏈接,可以從詳細(xì)內(nèi)容顯示頁面返回到簡(jiǎn)要信息列表頁面。圖6-8圖書詳細(xì)信息顯示頁面

【例6-15】

案例說明:圖書信息查詢使用BookList.xml存儲(chǔ)圖書簡(jiǎn)要信息,使用Book1.xml、Book2.xml、Book3.xml和Book4.xml存儲(chǔ)4本圖書的詳細(xì)信息。程序BookList.xsl將BookList.xml文檔轉(zhuǎn)換為圖6-7所示圖書簡(jiǎn)要信息列表。程序BookDetail.xsl可以分別將4個(gè)存儲(chǔ)圖書詳細(xì)信息的文檔轉(zhuǎn)換為圖6-8所示頁面。程序名稱:bookList.xml

01<?xmlversion="1.0"encoding="UTF-8"?>

02<?xml-stylesheettype="text/xsl"href="bookList.xsl"?>

03<Library>

04<CategoryType="計(jì)算機(jī)">

05<Bookid="1">

06<Title>軟件系統(tǒng)開發(fā)技術(shù)(修訂版)</Title>

07<Author>潘錦平,施小英,姚天昉</Author>

08<PubInfo>09<Publisher>西安電子科技大學(xué)出版社</Publisher>

10<PubDate>2002-01-01</PubDate>

11<ISBN>7-5606-0492-7</ISBN>

12<Price>16.00</Price>

13</PubInfo>

14<HLink>

15<LinkID>Book1.xml</LinkID>

16<LinkName>詳細(xì)</LinkName>

17</HLink>

18</Book>

19<Bookid="2">20<Title>軟件工程(第二版)</Title>

21<Author>鄧良松,劉海巖,陸麗娜</Author>

22<PubInfo>

23<Publisher>西安電子科技大學(xué)出版社</Publisher>

24<PubDate>2004-09-01</PubDate>

25<ISBN>7-5606-0924-4</ISBN>

26<Price>22.00</Price>

27</PubInfo>

28<HLink>29<LinkID>Book2.xml</LinkID>

30<LinkName>詳細(xì)</LinkName>

31</HLink>

32</Book>

33</Category>

34<CategoryType="小說">

35<Bookid="3">

36<Title>基督山伯爵(上下冊(cè))</Title>

37<Author>大仲馬(法)</Author>

38<PubInfo>39<Publisher>上海譯文出版社</Publisher>

40<PubDate>2001-08-01</PubDate>

41<ISBN>7-5327-2651-7</ISBN>

42<Price>38.00</Price>

43</PubInfo>

44<HLink>

45<LinkID>Book3.xml</LinkID>

46<LinkName>詳細(xì)</LinkName>

47</HLink>

48</Book>

49<Bookid="4">50<Title>諸葛孔明</Title>

51<Author>陳舜臣</Author>

52<

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論