DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> XML學習教程 >> XML詳解
XML 技術系列講座(12):驗證 XML 資料合法性(下)
編輯:XML詳解     
一、制定 Schema

考慮以下 XML 文件 books.xml:

<?xml version="1.0"?> <booklist> <book> <title>Straight Talk About Computers </title> <author>Lars Peterson</author> </book> <book> <title>You Can Combat Computer Stress</title> <author>Lars Peterson</author> <author>Carlos Diaz</author> </book> </booklist>

為上述 XML 文件制定 Schema 時,可以先為 <title> 與 <author> 元素制定規則,用 <ElementType> 定義它們為只能包含字串的文本元素:

<ElementType name="title" content="textOnly" dt:type="string" /> <ElementType name="author" content="textOnly" dt:type="string" />

接著說明 <book> 只能包含子元素,且子元素出現的順序固定,隨後在內部用 <element> 定義 <book> 包含一個 <title> 與任意個 <author> 子元素:

<ElementType name="book" content="eltOnly" order="seq"> <element type="title" /> <element type="author" maxOccurs="*" /> </ElementType>

最後用同樣的方法說明根元素 <booklist>:

<ElementType name="booklist" content="eltOnly"> <element type="book" minOccurs="0" maxOccurs="*" /> </ElementType>

下面的例子定義了「isbn」型別的屬性:資料類型是字串,並規定元素一旦引用該屬性,就必須賦值。

<AttributeType name="isbn" dt:type="string" required="yes" /> <ElementType name="book" content="eltOnly"> <attribute type="isbn" /> </ElementType> 二、XML 的可擴展性

由於 XML Schema 是一種內容「開放」的模型,這種可擴展性意味著使用者可以在 Schema 中使用自訂的元素與屬性。例如:

<ElementType name="price" xmlns:myExt="urn:myextensions" dt:type="float"> <myExt:salestax>17.5</myExt:salestax> <myExt:bulkbuy>20</myExt:bulkbuy> <myExt:discount>5.0</myExt:discount> </ElementType>

其中本地的屬性定義只聲明了 <price> 元素為「float」型別,而透過名稱空間又引用了另外三個標籤,分別說明書籍的銷售稅、批量定額與折扣率。這種擴展性增強了 Schema 的靈活性。

我們還可以用 DOM 技術來存取擴展的 Schema:

<book isbn="9-001-122-01"> <title>Straight Talk About Computers</title> <price>19.99</price> </book>

假設上述 XML 資料就是依剛才的 Schema 定義的,而且我們已用 DOM 定位了 <book> 元素,那麼接下來便能存取 <price> 節點,並利用其 definition 屬性從 Schema 中取得 <ElementType> 的定義:

Set priceNode = bookNode.selectSingleNode("price") Set priceElementTypeNode = priceNode.definition salestax = priceElementTypeNode.childNodes(0).text bulkbuy = priceElementTypeNode.childNodes(1).text discount = priceElementTypeNode.childNodes(2).text

如此一來,就能按需處理這些資料了。

三、在用戶端/伺服器環境中驗證 XML

在 C/S(用戶端/伺服器)環境中,用戶端與伺服器都能用 Schema 來驗證文件。在用戶端驗證的對象,可以是從伺服器收到的文件,也可以是即將送出的文件。下面的例子,就是用一个名為 CustomerSchema.xml 的 Schema 來驗證 docSubmit 的 XML 文件,然後傳送給伺服器:

Set rootnode = docSubmit.documentElement rootnode.setAttribute "xmlns", "x-schema:https://server/CustomerSchema.xml"

DOM 首先取得根元素的存取權,然後把 xmlns 設為 Schema 的地址,使之開始驗證。

相對於用戶端,在伺服器上進行驗證的意義更為重大,實務上也更為常見。由於連接到伺服器的用戶數眾多,有必要在處理它們送來的資料前,檢查收到的文件是否與預期的結構一致。驗證步驟如下:

1.將 XML 文件載入 DOM 樹,開始驗證:

Set docReceived = CreateObject("Microsoft.XMLDOM") docReceived.validateOnParse = True docReceived.async = False docReceived.load Request

2.判斷是否連結 XML Schema(只需檢查根元素中是否設定了 xmlns 屬性):

Set rootnode = docReceived.documentElement If rootnode.getAttribute("xmlns") = "x-schema:https://server/CustomerSchema.xml" Then   <!--有,開始驗證!--> Else   <!--沒有……--> End If

3.若沒有連結,伺服器就要指定一個 Schema 進行連結,然後將更新後的文件重新裝入另一個 DOM 樹中:

path = Server.mapPath("CustomerSchema.xml") attr = "x-schema:" & path rootnode.setAttribute "xmlns", attr Set docTested = CreateObject("Microsoft.XMLDOM") docTested.validateOnParse = True docTested.async = False docTested.loadXML docReceived.xml

由於 XML Schema 具備許多優秀特性,國際上許多著名公司與大型企業紛紛開始向 Schema 傾斜,例如微軟的 IE 5.0 就已支援 XML Schema。雖然 Schema 還需在不斷的實踐中逐步完善,但 XML Schema 取代 DTD 已是大勢所趨,因此筆者建議應學習並掌握這一技術。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved