XML - 資料交換管理/資料模式
XML - 資料交換管理
|
相關主題
|
參與進來
|
| 上一章 | 下一章 |
| ← 遞迴關係 | DTD → |
|
學習目標
|
發起者 佐治亞大學
|
資料模式是所有 XML 頁面的基礎。它們定義了物件、它們的關係、它們的屬性以及資料模型的結構。沒有它們,XML 文件將不存在。在本章中,您將瞭解 XML 資料模式的目的、它們錯綜複雜的部分以及如何使用它們。此外,還將包含示例供您在建立自己的資料模式時複製,從而使您的工作變得更加輕鬆。在本網頁的底部,包含了一個完整的模式,其中部分內容包含在本章的各個部分中。如果您想了解整個模式是如何作為一個整體工作的,請參考它。
資料模式,拋開所有技術細節,就是傳遞所有 XML 資訊的資料模型。它具有層次結構,從根元素(稍後將解釋)開始,一直延伸到覆蓋模型的每一個細節,並在中間包含詳細的步驟。資料模式主要包含兩個部分:實體及其關係。資料模式中包含的實體代表模型中的物件。它們具有唯一的識別符號、屬性和表示它們是什麼型別的物件的名稱。模式中的關係代表物件之間的關係,非常簡單。關係可以是一對一、一對多、多對多、遞迴以及您可以在資料模型中找到的任何其他型別。現在我們將開始建立自己的資料模式。
所有模式的開始方式都一樣,無論它們代表什麼型別的物件。每個模式的第一行都是此宣告
<?xml version="1.0" encoding="UTF-8"?>
示例 1:XML 宣告
示例 1 只是告訴瀏覽器或訪問此模式的任何檔案/程式,它是一個 XML 檔案並使用編碼結構“UTF-8”。您可以複製此程式碼來開始您自己的 XML 檔案。接下來是名稱空間宣告
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
示例 2:名稱空間宣告
名稱空間基本上是包含模式中大部分編碼的定義的字典。例如,在建立模式時,如果您宣告一個物件為“String”型別,那麼“String”型別的定義將包含在名稱空間中,以及它的所有屬性。對於您編寫的幾乎所有程式碼,情況都是如此。如果您建立或看過其他模式,大部分程式碼前面都是“xsd:”。一個很好的例子是“xsd:sequence”或“xsd:complexType”。sequence 和 complexType 都是定義在名稱空間中的物件,該名稱空間已連結到字首“xsd”。事實上,理論上您可以將預設名稱空間命名為任何名稱,只要您在整個模式中以相同的方式引用它即可。最常見的名稱空間包含大多數 XML 物件,是http://www.w3.org/2001/XMLSchema。現在來看示例 2。
第一部分讓任何檔案/程式都知道這是一個模式。很容易理解。就像 XML 宣告一樣,這是 XML 模式通用的,您可以將其用於您自己的模式。第二部分是實際的名稱空間宣告;xmlns 代表 XML NameSpace。這定義了模式的預設名稱空間,通常是程式碼中給出的名稱空間。同樣,我建議使用此程式碼來開始您的模式。最後一部分很難理解,但這裡有一個非常詳細的解釋。使用“unqualified”最適用,直到您遇到一些非常複雜的程式碼。
實體基本上是建立模式來表示的物件。如前所述,它們具有屬性和關係。我們現在將更深入地解釋它們到底是什麼以及如何為它們編寫程式碼。
實體有兩種型別:simpleType 和 complexType。simpleType 物件只有一個與其關聯的值。字串就是一個完美的 simpleType 物件示例,因為它只包含字串的值。大多數使用的 simpleType 將在預設名稱空間中定義;但是,您可以在模式的底部定義自己的 simpleType(這將在限制部分提到)。因此,您在模式中最常需要包含的物件只是 complexType。complexType 是一個具有多個與之關聯的屬性的物件,它可能包含也可能不包含與其關聯的子元素。這是一個 complexType 物件的示例
<xsd:complexType name="GenreType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="movie" type="MovieType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
示例 3:complexType 元素
此程式碼從 complexType 的宣告及其名稱開始。當其他實體引用它時,例如父元素,它將引用此名稱。第 2 行開始屬性和子元素的序列,它們都宣告為“element”。元素宣告為元素,程式碼行的第一部分,它們的名稱(其他文件將引用)包含在第二部分的“name”中。在第一個和第二個宣告之後是“type”宣告。請注意,對於name 和 description 元素,它們的型別是“xsd:string”,表明字串型別在“xsd”名稱空間中定義。對於movie 元素,型別是“MovieType”,因為“MovieType”前面沒有名稱空間,所以假設此型別包含在此模式中。(如果另一個模式包含在模式的頂部,它可能引用另一個模式中定義的型別。現在不用擔心)“minOccurs”和“maxOccurs”表示 Genre 和 MovieType 之間的關係。“minOccurs”可以是 0 或任意數字,僅取決於資料模型。“maxOccurs”可以是 1(一對一關係)、任意數字(一對多關係)或“unbounded”(一對多關係)。
每個模式都必須有一個根元素。此實體包含層次結構中其下方的所有其他實體。例如,在建立包含電影列表的模式時,根元素將類似於 MovieDatabase,或者可能是 MovieCollection,只是某種邏輯上包含所有其他物件(如型別、電影、演員、導演、劇情等)的東西。它總是從以下程式碼行開始:<xsd:element name="xxx">表示它是根元素,然後作為正常的 complexType 繼續。所有其他物件將以 simpleType 或 complexType 開頭。以下是一個 MovieDatabase 根元素的示例程式碼
<xsd:element name="MovieDatabase">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Genre" type="GenreType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
示例 4:根元素
這代表一個 MovieDatabase,其中 MovieDatabase 的子元素是 Genre。從那裡它繼續到電影等。我們將繼續使用這個例子來幫助你更好地理解。
父子關係是資料模式中的一個關鍵主題。它透過明確地闡述自上而下的配置,來表示資料模型層次結構的基本結構。看看這段程式碼,它顯示了電影如何與演員相關聯
<xsd:complexType name="MovieType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="actor" type="ActorType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ActorType">
<xsd:sequence>
<xsd:element name="lname" type="xsd:string"/>
<xsd:element name="fname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
示例 5:父子關係
在每個 MovieType 中,都有一個名為“actor”的元素,它是“ActorType”型別的。當 XML 文件填充資訊時,actor 周圍的標籤將是<actor></actor>,而不是<ActorType></ActorType>。為了使你的模式流暢且無錯誤,父元素中的type欄位將始終等於complexType 子元素宣告中的name欄位。
實體的屬性是 simpleType 物件,因為它只包含一個值。<xsd:element name="lname" type="xsd:string"/>是屬性的一個很好的例子。它被宣告為一個元素,與之相關聯一個名稱,並有一個型別宣告。本章附錄中包含了預設名稱空間中內建的眾多 simpleType 列表。屬性的使用非常簡單,直到你嘗試對其進行限制。
在某些情況下,某些資料必須遵守標準才能維護資料完整性。一個例子是社會安全號碼或電子郵件地址。如果你有一個包含傳送大量電子郵件的電子郵件地址的資料庫,則需要所有電子郵件地址都是有效的地址,否則每次傳送電子郵件時都會收到大量錯誤訊息。為了避免這個問題,你可以實質上獲取已知的 simpleType 並新增一個限制,以更好地滿足你的需求。現在你可以透過兩種方式做到這一點,但其中一種更簡單,更適合在資料模式中使用。你可以在父元素的宣告中編輯 simpleType,但這會很混亂,如果另一個模式想要使用它,則必須重新編寫程式碼。更好的方法是在模式底部列出一種新的型別,它會編輯之前已知的 simpleType。以下是一個使用社會安全號碼的示例
<xsd:simpleType name="emailaddressType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[^@]+@[^\.]+\..+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ssnType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-\d{2}-\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
示例 6:simpleType 上的限制
它包含在最後一個子元素下面的模式中,並在結束的</xsd:schema>之前。第一行聲明瞭 simpleType 併為其命名為“ssnType”。你可以隨意命名,只要你在整個模式中正確引用它即可。透過這樣做,你可以在模式中的任何位置或另一個模式中的任何位置使用此型別,前提是引用正確。第二行讓模式知道它是一個受限型別,並且它的基類是預設名稱空間中定義的字串。基本上,這種型別是一個字串,對它進行了限制,第三行是實際的限制。它可以是許多型別限制中的一種,它們列在本章附錄中。這種情況碰巧是“pattern”型別。“pattern”意味著 XML 文件中只允許使用特定順序的字元,它在value欄位中定義。此特定值表示三個數字,一個連字元,兩個數字,一個連字元,以及四個數字。要了解更多關於如何使用限制的資訊,請訪問 此連結,瞭解 W3 學校關於限制的部分。
<xsd:import>標籤用於匯入模式文件以及與模式文件中定義的資料型別相關的名稱空間。這允許 XML 模式文件使用名稱空間名稱(字首)來引用型別庫。讓我們仔細看看商店的簡單 XML 例項文件,該文件使用這些多個名稱空間名稱
<?xml version="1.0" encoding="UTF-8"?> <store:SimpleStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentourism.org/xmltext/SimpleStore.xsd" xmlns:store="http://www.opentourism.org/xmltext/Store" xmlns:MGR="http://www.opentourism.org/xmltext/CoreSchema"> <!-- Note the explicitly defined namespace declarations, the prefix store represents data types defined in the <code>http://www.opentourism.org/xmltext/Store.xml</code> namespace and the prefix MGR represents data types defined in the <code>http://www.opentourism.org/xmltext/CoreSchema</code> namespace. Also, notice that there is no default namespace declaration – every element and attribute must be associated with a namespace (we will see this is necessary weh we examine the schema document) --> <store:Store> <MGR:Name xmlns:MGR=" http://www.opentourism.org/xmltext/CoreSchema "> <MGR:FirstName>Michael</MGR:FirstName> <MGR:MiddleNames>Jay</MGR:MiddleNames> <MGR:LastName>Fox</MGR:LastName> </MGR:Name> <store:StoreName>The Gap</store:StoreName> <store:StoreAddress> <store:Street>86 Nowhere Ave.</store:Street> <store:City>Los Angeles</store:City> <store:State>CA</store:State> <store:ZipCode>75309</store:ZipCode> </store:StoreAddress> <!-- More store information would go here. --> </store:Store> <!-- More stores would go here. --> </store:SimpleStore>
示例 7 XML 例項文件 – [1]
讓我們看看模式文件,並瞭解如何使用<xsd:import>標籤從型別庫(外部模式文件)中匯入資料型別。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.opentourism.org/xmltext/Store.xml" xmlns:MGR="http://www.opentourism.org/xmltext/CoreSchema" targetNamespace="http://www.opentourism.org/xmltext/Store.xml" elementFormDefault="qualified"> <!-- The prefix MGR is bound to the following namespace name: <code>http://www.opentourism.org/xmltext/CoreSchema</code> The managerTypeLib.xsd schema document is imported by associating the schema with the <code>http://www.opentourism.org/xmltext/CoreSchema</code> namespace name, which was bound to the MGR prefix. The elementFormDefault attribute has the value ‘qualified' indicating that an XML instance document must use qualified names for every element(default namespace can not be used) --> <!-- The target namespace and default namespace are the same --> <xsd:import namespace="http://www.opentourism.org/xmltext/CoreSchema" schemaLocation="ManagerTypeLib.xsd"/> <xsd:element name="SimpleStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Store" type="StoreType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="StoreType"> <xsd:sequence> <xsd:element ref="MGR:Name"/> <xsd:element name="StoreName" type="xsd:string"/> <xsd:element name="StoreAddress" type="StoreAddressType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="StoreAddressType"> <xsd:sequence> <xsd:element name="Street" type="xsd:string"/> <xsd:element name="City" type="xsd:string"/> <xsd:element name="State" type="xsd:string"/> <xsd:element name="ZipCode" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
示例 8:XML 模式 [http://www.opentourism.org/xmltext/SimpleStore.xsd
與 include 標籤和 redefine 標籤一樣,import 標籤是將外部模式文件中的任何資料型別合併到另一個模式文件中的另一種方法,並且必須在任何元素或屬性宣告之前出現。當 XML 模式模組化並且型別庫在多個模式文件中維護和使用時,這些機制非常重要。
現在我們已經介紹了合併外部 XML 模式的所有三種方法,讓我們考慮這些機制的重要性。與大多數程式設計程式碼一樣,冗餘不受歡迎;對於自定義資料型別定義也是如此。如果已經存在可以應用於模式文件中元素的自定義資料型別,那麼在新的模式文件中再次建立它而不是使用它是否有意義?此外,如果你知道單個數據型別可以重複用於多個應用程式,那麼當你需要它時,你是否應該有一種引用該資料型別的方法?
模組化模式背後的理念是檢查你的模式做了什麼,確定哪些資料型別以某種形式頻繁使用,並開發一個型別庫。隨著你對更復雜模式的需求增加,你可以繼續新增你的庫,重複使用你的型別庫中的資料型別,並在需要時重新定義這些資料型別。這種重複使用的一個例子是客戶資訊模式——不同的部門將使用不同的模式,因為他們只需要部分客戶資訊。但是,大多數部門(如果不是所有部門的話)都需要一些特定的客戶資訊,例如姓名和聯絡資訊,這些資訊可以包含在各個部門模式文件中。
模式模組化是一種“最佳實踐”。透過維護型別庫以及重複使用和重新定義型別庫中的型別,你可以幫助確保你的 XML 模式文件不會變得過於龐大而難以閱讀。可讀性很重要,因為你可能不是唯一使用這些模式的人,重要的是其他人可以輕鬆地理解你的模式文件。
到目前為止,我們只討論了由全球資訊網聯盟 (W3C) 定義的 XML 模式。然而,還有其他方法可以定義 XML 例項文件中包含的資料,但我們只提到了兩種最流行和最知名的替代方法:文件型別定義 (DTD) 和 Relax NG 模式。
我們將在下一章介紹 DTD。Relax NG 模式更新,並且具有與 W3C XML 模式相同的許多功能;Relax NG 還聲稱更簡單、更容易學習,但這非常主觀。有關 Relax NG 的更多資訊,請訪問:http://www.relaxng.org/
首先是本章示例中使用的完整模式。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
<xsd:element name="MovieDatabase">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Genre" type="GenreType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="GenreType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="movie" type="MovieType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="MovieType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="rating" type="xsd:string"/>
<xsd:element name="director" type="xsd:string"/>
<xsd:element name="writer" type="xsd:string"/>
<xsd:element name="year" type="xsd:int"/>
<xsd:element name="tagline" type="xsd:string"/>
<xsd:element name="actor" type="ActorType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ActorType">
<xsd:sequence>
<xsd:element name="lname" type="xsd:string"/>
<xsd:element name="fname" type="xsd:string"/>
<xsd:element name="gender" type="xsd:string"/>
<xsd:element name="bday" type="xsd:string"/>
<xsd:element name="birthplace" type="xsd:string"/>
<xsd:element name="ssn" type="ssnType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ssnType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-\d{2}-\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
現在是時候回到起點……並回顧一下我們迄今為止涵蓋的所有模式資料型別、元素和屬性(以及我們可能沒有涵蓋的幾個屬性)。下表將詳細說明可以在 XML 模式中使用的 XML 資料型別、元素和屬性。
基本型別
這是一個包含所有基本型別的表格,您的模式中的屬性可以是這些型別。
| 型別 | 語法 | 合法值示例 | 約束面 |
| xsd:anyURI | <xsd:element name = “url” type = “xsd:anyURI” /> | http://www.w3.com | length, minLength, maxLength, pattern, enumeration, whitespace |
| xsd:boolean | <xsd:element name = “hasChildren” type = “xsd:boolean” /> | true 或 false 或 1 或 0 | pattern 和 whitespace |
| xsd:byte | <xsd:element name = “stdDev” type = “xsd:byte” /> | -128 到 127 | length, minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace 和 totalDigits |
| xsd:date | <xsd:element name = “dateEst” type = “xsd:date” /> | 2004-03-15 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration 和 whitespace |
| xsd:dateTime | <xsd:element name = “xMas” type = “xsd:dateTime” /> | 2003-12-25T08:30:00 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration 和 whitespace |
| xsd:decimal | <xsd:element name = “pi” type = “xsd:decimal” /> | 3.1415292 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace, fractionDigits 和 totalDigits |
| xsd:double | <xsd:element name = “pi” type = “xsd:double” /> | 3.1415292 或 INF 或 NaN | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:duration | <xsd:element name = “MITDuration” type = “xsd:duration” /> | P8M3DT7H33M2S | |
| xsd:float | <xsd:element name = “pi” type = “xsd:float” /> | 3.1415292 或 INF 或 NaN | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:gDay | <xsd:element name = “dayOfMonth” type = “xsd:gDay” /> | ---11 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:gMonth | <xsd:element name = “monthOfYear” type = “xsd:gMonth” /> | --02-- | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:gMonthDay | <xsd:element name = “valentine” type = “xsd:gMonthDay” /> | --02-14 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:gYear | <xsd:element name = “year” type = “xsd:gYear” /> | 1999 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:gYearMonth | <xsd:element name = “birthday” type = “xsd:gYearMonth” /> | 1972-08 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:ID | <xsd:attribute name="id" type="xsd:ID"/> | id-102 | length, minLength, maxLength, pattern, enumeration, 和 whitespace |
| xsd:IDREF | <xsd:attribute name="version" type="xsd:IDREF"/> | id-102 | length, minLength, maxLength, pattern, enumeration, 和 whitespace |
| xsd:IDREFS | <xsd:attribute name="versionList" type="xsd:IDREFS"/> | id-102 id-103 id-100 | length, minLength, maxLength, pattern, enumeration, 和 whitespace |
| xsd:int | <xsd:element name = “age” type = “xsd:int” /> | 77 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace 和 totalDigits |
| xsd:integer | <xsd:element name = “age” type = “xsd:integer” /> | 77 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:long | <xsd:element name = “cannelNumber” type = “xsd:int” /> | 214 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace |
| xsd:negativeInteger | <xsd:element name = “belowZero” type = “xsd:negativeInteger” /> | -123 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace 和 totalDigits |
| xsd:nonNegativeInteger | <xsd:element name = “numOfchildren” type = “xsd:nonNegativeInteger” /> | 2 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace 和 totalDigits |
| xsd:nonPositiveInteger | <xsd:element name = “debit” type = “xsd:nonPositiveInteger” /> | 0 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace 和 totalDigits |
| xsd:positiveInteger | <xsd:element name = “credit” type = “xsd:positiveInteger” /> | 500 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace 和 totalDigits |
| xsd:short | <xsd:element name = “numOfpages” type = “xsd:short” /> | 476 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace 和 totalDigits |
| xsd:string | <xsd:element name = “name” type = “xsd:string” /> | Joeseph | length, minLength, maxLength, pattern, enumeration, whitespace 和 totalDigits |
| xsd:time | <xsd:element name = “credit” type = “xsd:time” /> | 13:02:00 | minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, 和 whitespace, |
模式元素
( 來自 https://w3schools.tw/schema/schema_elements_ref.asp )
以下列出了所有可以包含在您的模式中的元素。
| 元素 | 解釋 |
| all | 指定子元素可以按任何順序出現。每個子元素都可以出現 0 或 1 次 |
| annotation | 指定模式註釋的頂層元素 |
| any | 使作者能夠使用模式未指定的元素擴充套件 XML 文件 |
| anyAttribute | 使作者能夠使用模式未指定的屬性擴充套件 XML 文件 |
| appInfo | 指定要由應用程式使用的資訊(必須位於註釋內部) |
| attribute | 定義一個屬性 |
| attributeGroup | 定義一個屬性組,用於複雜型別定義 |
| choice | 僅允許在包含元素記憶體在 <choice> 宣告中包含的元素之一 |
| complexContent | 定義對包含混合內容或僅包含元素的複雜型別的擴充套件或限制 |
| complexType | 定義一個複雜型別元素 |
| documentation | 定義模式中的文字註釋(必須位於註釋內部) |
| element | 定義一個元素 |
| extension | 擴充套件現有的 simpleType 或 complexType 元素 |
| field | 指定用於定義標識約束的值的 XPath 表示式 |
| group | 定義一組元素,用於複雜型別定義 |
| import | 將具有不同目標名稱空間的多個模式新增到文件 |
| include | 將具有相同目標名稱空間的多個模式新增到文件 |
| key | 指定屬性或元素值作為例項文件中包含元素內的鍵(唯一、不可為空且始終存在) |
| keyref | 指定屬性或元素值與指定鍵或唯一元素的那些值對應 |
| list | 將簡單型別元素定義為值的列表 |
| notation | 描述 XML 文件中非 XML 資料的格式 |
| redefine | 從外部模式重新定義簡單型別和複雜型別、組和屬性組 |
| restriction | 定義對 simpleType、simpleContent 或 complexContent 的限制 |
| schema | 定義模式的根元素 |
| selector | 指定選擇一組元素以進行標識約束的 XPath 表示式 |
| sequence | 指定子元素必須按順序出現。每個子元素可以出現 0 到任意多次 |
| simpleContent | 包含對純文字複雜型別或簡單型別的擴充套件或限制作為內容,並且不包含任何元素 |
| simpleType | 定義一個簡單型別,並指定關於屬性或純文字元素的值的約束和資訊 |
| union | 將簡單型別定義為從指定簡單資料型別收集(聯合)的值 |
| unique | 定義元素或屬性值在範圍內必須是唯一的 |
模式資料型別限制和麵
( 來自 https://w3schools.tw/schema/schema_elements_ref.asp )
以下列出了所有可以包含在您的模式中的限制類型。
| 約束 | 描述 |
| enumeration | 定義一個可接受值的列表 |
| fractionDigits | 指定允許的小數位數的最大值。必須等於或大於零 |
| length | 指定允許的字元或列表項的精確數量。必須等於或大於零 |
| maxExclusive | 指定數值的上限(值必須小於該值) |
| maxInclusive | 指定數值的上限(值必須小於或等於該值) |
| maxLength | 指定允許的字元或列表項的最大數量。必須等於或大於零 |
| minExclusive | 指定數值的下限(值必須大於該值) |
| minInclusive | 指定數值的下限(值必須大於或等於該值) |
| minLength | 指定允許的字元或列表項的最小數量。必須等於或大於零 |
| pattern | 定義可接受的字元的精確順序 |
| totalDigits | 指定允許的數字的精確數量。必須大於零 |
| whiteSpace | 指定如何處理空格(換行符、製表符、空格和回車符) |
正則表示式
可以使用特殊的正則表示式(regex)語言構建模式。XML 模式中的正則表示式語言基於 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}] | 後面可以跟任何(或沒有)單詞字元和一個@符號, | 例如:my@ |
| [w+] | 後面至少要有一個單詞字元, | 例如:mail |
| [W*] | 後面可以跟零個、一個或多個非單詞字元, | 例如:_ |
| [w+.] | 後面至少要有一個單詞字元和一個句號, | 例如:please. |
| [w+.*] | 後面可以跟零到無限次前面的字串, | 例如:opentourism. |
| [w*] | 最後跟零個、一個或多個單詞字元 | 例如:org |
| email-address: answer-my@mail_please.opentourism.org | ||
例項文件屬性
這些屬性不需要在模式中宣告
| 屬性 | 解釋 | 示例 |
| xsi:nil | 表示某個元素沒有值或值未知。該元素必須在模式文件中設定為 nillable <xsd:element name=”last_name” type=”xsd:string” nillable=true”/> |
<full_name xmlns:xsi= ”http://www.w3.org/2001/XMLSchema-instance”> <first_name>Madonna</first_name> <last_name xsi:nil=”true”/> </full_name> |
| xsi:noNamespaceSchemaLocation | 定位不在任何名稱空間中的元素的模式 | <radio xsi:noNamespaceSchemaLocation= ”http://www.opentourism.org/xmtext/radio.xsd”> <!—radio stuff goes here -- > </radio> |
| xsi:schemaLocation | 定位位於指定名稱空間中的元素和屬性的模式 | <radio xmlns= ”http://www.opentourism.org/xmtext/NS/radio xmlns:xsi= ”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation= ”http://www.arches.uga.eduNS/radio” ”http://www.opentourism.org/xmtext/radio.xsd”> <!—radio stuff goes here -- > </radio> |
| xsi:type | 可以在例項文件中用於指示元素的型別。 | <height xsi:type=”xsd:decimal”>78.9</height> |
有關 XML 模式結構、資料型別和工具的更多資訊,您可以訪問 http://www.w3.org/XML/Schema。