跳轉到內容

XML - 管理資料交換/C.3

來自華夏公益教科書

XML 樣式表的更多功能

[編輯 | 編輯原始碼]

在上一章中,建立了一個樣式表,它只是將 XML 文件中的文字輸出到 HTML 頁面。可以使用表格格式化文件的顯示,以佈局內容以供顯示。此外,可以使用 HTML 中的 style 標籤定義不同的背景顏色、字型大小、字型粗細和對齊方式,以設計輸出頁面。表 3-3 顯示了提到的功能。注意,HTML 命令為大寫。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Tour Guide</TITLE> <--The style tag can be used to define different colors, font size, font family, text alignment, margins, and many other style-formatting elements just like a HTML page.--> <STYLE TYPE="text/css"> H2 {TEXT-ALIGN:CENTER;} .greenBackground {BGCOLOR=“LIGHTGREEN; TEXT-ALIGN:CENTER;} .yellowBackground {BGCOLOR=“YELLOW; TEXT-ALIGN:CENTER; FONT-WEIGHT: BOLD ; FONT-SIZE:14pt;} .salmonBackground {BGCOLOR=“LIGHTSALMON; TEXT-ALIGN:CENTER; FONT-SIZE:12pt;} </STYLE> </HEAD> <BODY> <H2>Cities and Hotels</H2> <--The template defined is called to display in a HTML page by the xsl:apply-templates element--> <xsl:apply-templates select="tourGuide"/> </BODY> </HTML> </xsl:template> <xsl:template match="tourGuide"> <TABLE BORDER="1" WIDTH="100%"> <--There is a xsl:for-each element, hotel, nested inside the xsl:for-each city element. (Display the appropriate contents for the hotel elements for every hotel inside every city).--> <xsl:for-each select="city"> <TR> <TD COLSPAN="6" CLASS="greenBackground"> <BR/> <xsl:text>Continent: </xsl:text> <xsl:value-of select="continent"/> <BR/> <xsl:text>Country: </xsl:text> <xsl:value-of select="country"/> <BR/> <xsl:text>Administration Unit: </xsl:text> <xsl:value-of select="adminUnit"/> <BR/> <xsl:text>City: </xsl:text> <xsl:value-of select="cityName"/> <BR/> <BR/> </TD> </TR> <TR CLASS="yellowBackground"> <TD/> <TD> <xsl:text>Hotel Name</xsl:text> </TD> <TD> <xsl:text>Street Address</xsl:text> </TD> <TD> <xsl:text>Telephone Number</xsl:text> </TD> <TD> <xsl:text>Email Address</xsl:text> </TD> <TD> <xsl:text>Hotel Rating</xsl:text> </TD> </TR> <xsl:for-each select="hotel"> <TR CLASS="salmonBackground"> <TD STYLEFONT-SIZE:8pt"> <--An image tag is used to display an image--> <--Just like the src, the attributes width, height, and alt can be called to format the image.--> <IMG> <xsl:attribute name="SRC"> <--The file name and location is defined as an attribute (filename) of the empty tag, hotelPicture, in the XML schema. To indicate that filename is the data source, the src attribute of the image tag must be called--> <xsl:value-of select="hotelPicture/@filename"/> </xsl:attribute> <xsl:attribute name="WIDTH"> <xsl:value-of select="hotelPicture/@size"/> </xsl:attribute> <xsl:attribute name="HEIGHT"> <xsl:value-of select="hotelPicture/@size"/> </xsl:attribute> <xsl:attribute name="ALT"> <xsl:value-of select="hotelPicture/@value"/> </xsl:attribute> </IMG> <BR/> <xsl:value-of select="hotelPicture/@imageURL"/> </TD> <TD> <xsl:value-of select="hotelName"/> </TD> <TD> <xsl:value-of select="streetAddress"/> </TD> <TD> <xsl:value-of select="telephoneNumber"/> </TD> <TD> <xsl:value-of select="emailAddress"/> </TD> <TD> <xsl:value-of select="hotelRating"/> </TD> </TR> </xsl:for-each> <TR CLASS="salmonBackground"> <TD COLSPAN="6" TEXT-ALIGN:RIGHT"> <H3> <xsl:text>Number of hotel(s) found: </xsl:text> <--<xsl:value-of select="count(hotel)"/>, count() is a built-in function that counts the number of node indicated by the parameter, in this case the number of hotels. This means that the total number of hotels for each city is reported.--> <xsl:value-of select="count(hotel)"/> </H3> </TD> </TR> </xsl:for-each> </TABLE> </xsl:template> </xsl:stylesheet>


注意:註釋標籤格式不正確。為了讓它們顯示在螢幕上,需要進行一些細微的調整。您需要新增  ! 以確保您可以驗證您的樣式表。
示例:<!-- 這是一個名為自定義簡單型別,從 Hotel 呼叫,當有人輸入電子郵件地址時 -->


表 3-3:用於一對多關係的 XML 樣式表


可以使用 XML 樣式表使用 HTML 表格格式化輸出,以進行佈局。

參考第 2 章 - 使用 Netbeans 建立上述 XML 樣式表的步驟的單個實體。

使用 city_hotel.xml 驗證 city_hotel.xsd 的上述樣式表 (city_hotel.xsl) 的輸出結果:連結到 city_hotel.html

正則表示式

[編輯 | 編輯原始碼]

可以使用特殊的正則表示式 (regex) 語言來構造模式。XML Schema 中的正則表示式語言基於 Perl 的正則表示式語言。以下是常見的一些符號

. (句號)表示任何字元
\d表示任何數字
\D表示任何非數字
\w表示任何單詞(字母數字)字元
\W表示任何非單詞字元(例如 -, +, =)
\s表示任何空格(包括空格、製表符、換行符和回車符)
\S表示任何非空格字元
x*表示零個或多個 x
(xy)*表示零個或多個 xy
x+表示 x 的重複,至少一次
x?表示一個或零個 x
(xy)?表示一個或沒有 xy
[abc]表示包含一組值中的一個
[0-9] 表示包含從 0 到 9 的值的範圍
x{5}表示恰好有 5 個 x(連續的)
x{5,}表示至少有 5 個 x(連續的)
x{5,8}表示至少 5 個但最多 8 個 x(連續的)
(xyz){2}表示恰好有 2 個 xyz(連續的)

例如,驗證社會安全號碼的模式是 \d{3}-\d{2}-\d{4}

先前的模式程式碼對 emailAddressType \w+\W*\w*@{1}\w+\W*\w+.\w+.*\w* 進行了檢查
[w+]表示至少一個單詞(字母數字)字元,例如:answer
[W*]後面跟著零個、一個或多個非單詞字元,例如:-
[w*@{1}]後面跟著任何(或沒有)單詞字元和一個 at 符號,例如:my@
[w+]後面跟著至少一個單詞字元,例如:mail
[W*]後面跟著零個、一個或多個非單詞字元,例如:_
[w+.]後面跟著至少一個單詞字元和一個句號,例如:please.
[w+.*]零到無限次,後面跟著前面的字串,例如:opentourism.
[w*]最後跟著零個、一個或多個單詞字元例如:org
電子郵件地址:answer-my@mail_please.opentourism.org


Netbeans 中的常見錯誤

[編輯 | 編輯原始碼]
  • 上一章中列出的錯誤對於本章中的錯誤也很有用。
華夏公益教科書