XML 架構
| 華夏公益教科書使用者認為此頁面應該拆分為更小的頁面,並具有更窄的子主題。 您可以透過將此大頁面拆分為更小的頁面來提供幫助。請確保遵循命名規則。將書籍分為更小的部分可以提供更多重點,並允許每個部分專注於做好一件事,這對每個人都有益。 |
| 讀者請求擴充套件此書籍以包含更多資料。 您可以透過新增新資料(學習方法)或在閱覽室中尋求幫助。 |
歡迎來到XML 架構書籍。它描述了 XML 架構的結構,並解釋瞭如何使用 XML 架構來驗證 XML 文件。
閱讀本書的學生應該已經熟悉XML 的基本原理,並對資料型別 有一定的瞭解。
只有字首、根元素:<xs:schema> 和元素 部分是構建 XML 架構所必需的。可以跳過XML 架構的歷史、XML 架構用於什麼?、當 XML 架構在驗證複雜規則方面變得效率低下時 和XML 架構示例,後面的部分是從最重要的部分開始的附加資訊。
XML 架構 是由全球資訊網聯盟 (W3C) 建立的標準http://www.w3c.org。與 DTD 不同,XML 架構使用 XML 檔案格式來定義 XML 架構本身。一旦你學習了 XML 檔案的結構,你就不需要學習另一種語法。
XML 架構主要用於驗證 XML 文件的結構和資料型別。就結構而言,XML 架構定義了
- 預期哪些資料元素
- 預期資料元素的順序
- 這些資料元素的巢狀情況
- 哪些資料元素是可選的,哪些資料元素是必需的
XML 架構還可以被 XML 資料對映工具用於快速從資料庫中提取資料並將其轉移到 XML 檔案中。
最好的類比之一是藍圖類比。就像建築藍圖描述房屋的結構設計一樣,XML 架構提供了檔案的“結構設計”。
儘管 XML 架構非常擅長對資料元素和資料型別進行順序驗證,但它們在表達高度複雜的業務規則方面往往很笨拙。例如,當你到達一個大型檔案的末尾時,很難制定一條規則來檢查一個數據元素是否具有另一個數據元素在檔案開頭應該具有的另一個值。這可以透過使用 XML 轉換和 XPath 表示式來實現。
以下是一個完整的 XML 架構檔案的示例,用於個人聯絡人。
| XML 架構檔案 | 符合要求的 XML 例項檔案示例 |
|---|---|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample"
xmlns:tns="http://www.example.org/contactExample"
elementFormDefault="qualified">
<xs:element name="Contacts">
<xs:annotation>
<xs:documentation>
File of personal contacts.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
|
<?xml version="1.0" encoding="UTF-8"?>
<ex:Contacts xmlns:ex="http://www.example.org/contactExample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd ">
<ex:Person>
<ex:PersonGivenName/>
<ex:PersonFamilyName/>
<ex:PersonBirthDate/>
</ex:Person>
</ex:Contacts>
|
與任何其他 XML 檔案一樣,XML 架構檔案通常以 XML 宣告 (<?xml version="1.0"?>) 開頭,後面是根元素 (始終是 xs:schema)。
儘管可以使用任何字首來引用名稱空間http://www.w3.org/2001/XMLSchema,但最常見的約定是使用“xs”。有些人更喜歡“xsd”;有些人更喜歡使用預設名稱空間(這意味著不需要使用字首)。所有 XML 架構元素都在此名稱空間中。
所有 XML 架構檔案都必須以 <xs:schema> 標記開始和結束。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample"
xmlns:tns="http://www.example.org/contactExample"
elementFormDefault="qualified">
...other structure here...
</xs:schema>
|
<?xml version="1.0" encoding="UTF-8"?>
...any elements here...
|
架構必須以</xs:schema>結束標記結尾。
XML 架構定義了在名稱空間(例如 http://www.example.org/contactExample)中可用的元素和屬性。在 XML 架構中,此名稱空間使用targetNamespace屬性定義。
在 XML 檔案中,可以使用xmlns屬性匯入名稱空間(xmlns代表XML NameSpace)。xmlns屬性名稱可以以:和字首(例如xs)結尾。在這種情況下,匯入的標籤必須使用此字首。字首用於區分來自不同名稱空間的相同名稱的標籤。
您可以看到,我們在示例中定義的目標名稱空間是 XML 檔案中匯入的名稱空間之一。您可以看到,我們使用tns字首匯入文件本身的名稱空間,以便我們在文件中定義的元素可以從tns:開始在文件本身中使用。
<xs:sequence>用於元素的有序組。對於無序的元素組,請使用<xs:all>。
元素在 XML 架構中使用<xs:element>標記定義
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample"
xmlns:tns="http://www.example.org/contactExample"
elementFormDefault="qualified">
<xs:element name="Contacts"/>
</xs:schema>
|
<?xml version="1.0" encoding="UTF-8"?>
<ex:Contacts xmlns:ex="http://www.example.org/contactExample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd "/>
|
對於具有文字主體的元素,可以使用type屬性定義文字的型別
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="PersonGivenName" type="xs:string"/>
...
|
...
<ex:PersonGivenName>Sue</ex:PersonGivenName>
...
|
...
<xs:element name="PersonFamilyName" type="xs:string"/>
...
|
...
<ex:PersonFamilyName>Smith</ex:PersonFamilyName>
...
|
...
<xs:element name="PersonBirthDate" type="xs:date"/>
...
|
...
<ex:PersonBirthDate>1990-09-23</ex:PersonBirthDate>
...
|
以下是常見的 XML 架構基本資料型別
| 型別 | 描述 | 用法 | 合法值示例 |
|---|---|---|---|
| xs:anyURI | <xs:element name = “url” type = “xs:anyURI” /> | http://www.w3.com | |
| xs:boolean | <xs:element name = “hasChildren” type = “xs:boolean” /> | true 或 false 或 1 或 0 | |
| xs:byte | <xs:element name = “stdDev” type = “xs:byte” /> | -128 到 127 | |
| xs:date | <xs:element name = “dateEst” type = “xs:date” /> | 2004-03-15 | |
| xs:dateTime | <xs:element name = “xMas” type = “xs:dateTime” /> | 2003-12-25T08:30:00 | |
| xs:decimal | <xs:element name = “pi” type = “xs:decimal” /> | 3.1415292 | |
| xs:double | <xs:element name = “pi” type = “xs:double” /> | 3.1415292 或 INF 或 NaN | |
| xs:duration | <xs:element name = “MITDuration” type = “xs:duration” /> | P8M3DT7H33M2S | |
| xs:float | <xs:element name = “pi” type = “xs:float” /> | 3.1415292 或 INF 或 NaN | |
| xs:gDay | <xs:element name = “dayOfMonth” type = “xs:gDay” /> | ---11 | |
| xs:gMonth | <xs:element name = “monthOfYear” type = “xs:gMonth” /> | --02(格式 --02-- 通常被列出,但這是錯誤的) | |
| xs:gMonthDay | <xs:element name = “valentine” type = “xs:gMonthDay” /> | --02-14 | |
| xs:gYear | <xs:element name = “year” type = “xs:gYear” /> | 1999 | |
| xs:gYearMonth | <xs:element name = “birthday” type = “xs:gYearMonth” /> | 1972-08 | |
| xs:ID | <xs:attribute name="id" type="xs:ID"/> | id-102 | |
| xs:IDREF | <xs:attribute name="version" type="xs:IDREF"/> | id-102 | |
| xs:IDREFS | <xs:attribute name="versionList" type="xs:IDREFS"/> | id-102 id-103 id-100 | |
| xs:int | <xs:element name = “age” type = “xs:int” /> | 77 | |
| xs:integer | <xs:element name = “age” type = “xs:integer” /> | 77 | |
| xs:long | <xs:element name = “cannelNumber” type = “xs:long” /> | 214 | |
| xs:negativeInteger | <xs:element name = “belowZero” type = “xs:negativeInteger” /> | -123 | |
| xs:nonNegativeInteger | <xs:element name = “numOfchildren” type = “xs:nonNegativeInteger” /> | 2 | |
| xs:nonPositiveInteger | <xs:element name = “debit” type = “xs:nonPositiveInteger” /> | 0 | |
| xs:positiveInteger | <xs:element name = “credit” type = “xs:positiveInteger” /> | 500 | |
| xs:short | <xs:element name = “numOfpages” type = “xs:short” /> | 476 | |
| xs:string | <xs:element name = “name” type = “xs:string” /> | Joseph | |
| xs:time | <xs:element name = “credit” type = “xs:time” /> | 13:02:00 |
可以使用<xs:simpleType/>和<xs:restriction/>標記對資料型別定義一些架構限制和方面。例如,可以使用<xs:length/>標記將字串型別的主體文字固定為 5 個字元的長度,如上所示
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="PersonGivenName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
...
|
...
<ex:PersonGivenName>Kevin</ex:PersonGivenName>
...
|
...
<ex:PersonGivenName>Helen</ex:PersonGivenName>
...
|
以下是所有可使用的架構限制和方面
| 限制 | 解釋 | 允許的限制值 |
|---|---|---|
| minExclusive | 指定對於任何有序資料型別,比所有允許值都小的值 | 任何整數、小數、日期或時間 |
| minInclusive | 指定對於任何有序資料型別,允許的最小值 | 任何整數、小數、日期或時間 |
| maxExclusive | 指定對於任何有序資料型別,比所有允許值都大的值 | 任何整數、小數、日期或時間 |
| maxInclusive | 指定對於任何有序資料型別,允許的最大值 | 任何整數、小數、日期或時間 |
| totalDigits | 指定允許的小數點左側和小數點右側的最大位數 | 正整數 |
| fractionDigits | 指定允許的小數點右側的最大位數 | 非負整數 |
| length | 指定允許的精確字元數 | 非負整數 |
| minLength | 指定允許的最小字元數 | 非負整數 |
| maxLength | 指定允許的最大字元數 | 非負整數 |
| enumeration | 指定允許的值或允許值集 | 任何 |
| whiteSpace | 如果指定 "preserve" 值,則任何字元都將按原樣保留。 如果指定 "replace" 值,則任何製表符 (#x9)、換行符 (#xA) 和回車符 (#xD) 都將替換為空格 (#x20)。 如果指定 "collapse" 值,則任何製表符、換行符和回車符也將替換為空格, 連續空格將合併成單個空格,並且開頭和結尾空格將被刪除。 |
"preserve"、"replace" 或 "collapse" |
| pattern | 指定值必須匹配的正則表示式 | 正則表示式 |
任何可以具有子元素和/或屬性的元素型別都被視為複雜元素。使用<xs:complexType/>標記定義複雜元素型別。
子元素使用不同的出現規則定義。
子元素可以在<xs:all/>標記中定義:子元素必須全部存在,並且可以按任意順序出現。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:all>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:all>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person>
<ex:PersonBirthDate/>
<ex:PersonFamilyName/>
<ex:PersonGivenName/>
</ex:Person>
...
|
子元素可以在<xs:sequence/>標記中定義:子元素必須按相同的順序出現。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:sequence>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person>
<ex:PersonGivenName/>
<ex:PersonFamilyName/>
<ex:PersonBirthDate/>
</ex:Person>
...
|
子元素可以在<xs:choice/>標記中定義:必須出現一個且只有一個子元素。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:choice>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:choice>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person>
<ex:PersonGivenName/>
</ex:Person>
...
|
...
<ex:Person>
<ex:PersonFamilyName/>
</ex:Person>
...
| |
...
<ex:Person>
<ex:PersonBirthDate/>
</ex:Person>
...
|
一些出現規則可以包含在其他出現規則中。<xs:choice/>標記和<xs:sequence/>標記可以包含在<xs:choice/>標記或<xs:sequence/>標記中。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:choice>
<xs:element name="PersonBirthDate" />
<xs:element name="PersonAge" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person>
<ex:PersonGivenName />
<ex:PersonFamilyName />
<ex:PersonAge />
</ex:Person>
...
|
...
<ex:Person>
<ex:PersonGivenName />
<ex:PersonFamilyName />
<ex:PersonBirthDate />
</ex:Person>
...
|
可以使用<xs:element/>標記的minOccurs和maxOccurs屬性更改出現次數。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonGivenName" minOccurs="0" maxOccurs="1"/>
<xs:element name="PersonFamilyName" minOccurs="3" maxOccurs="unbounded"/>
<xs:element name="PersonBirthDate" minOccurs="2" maxOccurs="2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person>
<ex:PersonFamilyName/>
<ex:PersonFamilyName/>
<ex:PersonFamilyName/>
<ex:PersonFamilyName/>
<ex:PersonBirthDate/>
<ex:PersonBirthDate/>
</ex:Person>
...
|
預設情況下,最小出現次數為 1,最大出現次數為 1。要定義無限次出現,請指定unbounded。
複雜元素可以在其主體中包含文字,這些文字位於其子元素之前、之間和之後,將mixed屬性設定為true。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
</xs:sequence>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person>
You can add text before,
<ex:PersonGivenName />
between,
<ex:PersonFamilyName />
and after the sub-elements.
</ex:Person>
...
|
預設情況下,mixed屬性設定為false。
可以使用<xs:attribute/>標記定義元素的屬性。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:attribute name="relation"/>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person relation="a colleague"/>
...
|
如果元素同時包含屬性和子元素,則<xs:attribute/>標記必須定義在<xs:all/>、<xs:sequence/>或<xs:choice/>標記之上。資料型別、限制和方面可以像僅文字主體元素一樣為屬性定義。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonGivenName" />
</xs:sequence>
<xs:attribute name="professional"
type=“xs:boolean”/>
<xs:attribute name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="woman"/>
<xs:enumeration value="man"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person professional="true" gender="woman">
<ex:PersonGivenName />
</ex:Person>
...
|
預設情況下,屬性是可選的。這可以使用use屬性更改。
- 如果其值為
optional,則可以省略屬性。 - 如果其值為
required,則屬性必須存在。
可以使用default屬性定義預設值。如果屬性不存在,解析器將認為屬性存在,其值為預設值。
可以使用fixed屬性將屬性限制為一個常量值。由於fixed屬性也用作預設值,因此您不能同時定義default屬性。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:attribute name="professional" use=“required”/>
<xs:attribute name="gender" fixed="woman"/>
<xs:attribute name="comment" default="no comment"/>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person professional="true" comment="supplier"/>
...
|
...
<ex:Person gender="woman" professional="false"/>
...
|
元素可以使用<xs:simpleContent/>標記同時包含屬性和文字內容(請記住,簡單型別元素不能包含屬性)。
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
...
<xs:element name="Person">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:positiveInteger">
<xs:attribute name="lastUpdate" type="xs:date" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
...
|
...
<ex:Person lastUpdate="2009-09-10">
1864
</ex:Person>
...
|
簡單型別和複雜型別可以在元素樹之外定義。在這種情況下,<xs:element/>標記沒有主體,保留其name屬性,並有一個type屬性。然後在根元素之外定義<xs:complexType/>標記,其name屬性包含元素型別名稱。對XML檔案驗證沒有變化。讓我們看一下這個XML模式
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample"
xmlns:tns="http://www.example.org/contactExample"
elementFormDefault="qualified">
<xs:element name="Contacts">
<xs:complexType>
<xs:sequence>
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="LastUpdate">
<xs:simpleType>
<xs:restriction base="xs:date">
<xs:minExclusive value="2003-05-06"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
|
<?xml version="1.0" encoding="UTF-8"?>
<ex:Contacts xmlns:ex="http://www.example.org/contactExample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd"
LastUpdate="2003-05-07">
<ex:Person>
<ex:PersonGivenName/>
<ex:PersonFamilyName/>
<ex:PersonBirthDate/>
</ex:Person>
</ex:Contacts>
|
現在,讓我們在根元素樹之外定義Person複雜型別和LastUpdate簡單型別
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample"
xmlns:tns="http://www.example.org/contactExample"
elementFormDefault="qualified">
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="LastUpdateType">
<xs:restriction base="xs:date">
<xs:minExclusive value="2003-05-06"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="Contacts">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" type="PersonType"/>
</xs:sequence>
<xs:attribute name="LastUpdate" type="LastUpdateType"/>
</xs:complexType>
</xs:element>
</xs:schema>
|
<?xml version="1.0" encoding="UTF-8"?>
<ex:Contacts xmlns:ex="http://www.example.org/contactExample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd"
LastUpdate="2003-05-07">
<ex:Person>
<ex:PersonGivenName/>
<ex:PersonFamilyName/>
<ex:PersonBirthDate/>
</ex:Person>
</ex:Contacts>
|
複雜型別和簡單型別可以以任何順序定義。定義的型別可以在模式的不同元素中重複使用,從而避免重複其描述。它避免了XSD檔案過度縮排。此外,使用型別定義,元素不僅具有名稱,而且還具有型別名稱,該型別名稱也可以用作類名稱。某些用於根據XML模式解析XML內容的工具可能需要複雜型別元素的型別名稱。
元素和屬性可以使用引用重複使用。在這種情況下,<xs:element/>標記或<xs:attribute/>標記沒有主體,沒有name屬性,並有一個ref屬性。此ref屬性包含另一個元素或另一個屬性的名稱。讓我們在之前示例的Person元素上使用一個引用
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample"
xmlns:tns="http://www.example.org/contactExample"
elementFormDefault="qualified">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="LastUpdateType">
<xs:restriction base="xs:date">
<xs:minExclusive value="2003-05-06"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="Contacts">
<xs:complexType>
<xs:sequence>
<xs:element ref="Person"/>
</xs:sequence>
<xs:attribute name="LastUpdate" type="LastUpdateType"/>
</xs:complexType>
</xs:element>
</xs:schema>
|
<?xml version="1.0" encoding="UTF-8"?>
<ex:Contacts xmlns:ex="http://www.example.org/contactExample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd"
LastUpdate="2003-05-07">
<ex:Person>
<ex:PersonGivenName/>
<ex:PersonFamilyName/>
<ex:PersonBirthDate/>
</ex:Person>
</ex:Contacts>
|
上面單獨型別定義與使用引用之間的區別在於,任何元素或屬性都可以被引用。此外,使用引用,連結是使用名稱而不是型別名稱完成的。這意味著我們連結的不是類,而是類的例項。
定義的複雜型別可以重複使用,新增子元素或屬性。然後擴充套件複雜型別。可以使用<xs:complexContent/>和<xs:extension/>標記來完成此操作。擴充套件型別名稱在<xs:extension/>標記的base屬性中定義。以下是一個示例,其中PersonType複雜型別透過為Person元素新增professional屬性來擴充套件
| XML 架構檔案 | 符合要求的 XML 檔案示例 |
|---|---|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample"
xmlns:tns="http://www.example.org/contactExample"
elementFormDefault="qualified">
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="PersonGivenName" />
<xs:element name="PersonFamilyName" />
<xs:element name="PersonBirthDate" />
</xs:sequence>
</xs:complexType>
<xs:element name="Contacts">
<xs:complexType>
<xs:sequence>
<xs:element name="Person">
<xs:complexType>
<xs:complexContent>
<xs:extension base="PersonType">
<xs:attribute name="professional" type="xs:boolean"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
|
<?xml version="1.0" encoding="UTF-8"?>
<ex:Contacts xmlns:ex="http://www.example.org/contactExample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/contactExample contactExample.xsd">
<ex:Person professional="true">
<ex:PersonGivenName/>
<ex:PersonFamilyName/>
<ex:PersonBirthDate/>
</ex:Person>
</ex:Contacts>
|
可以像這樣定義具有公共和不同子元素或屬性的各種元素。公共項將在一個公共複雜型別中定義,不同項將在擴充套件第一個複雜型別的不同複雜型別中定義。
| 屬性 | 解釋 | 示例 |
|---|---|---|
| xsi:nil | 指示某個元素沒有值或值未知。 元素必須在模式文件中設定為可為空 <xs:element name=”last_name” type=”xs: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=”xs:decimal”>78.9</height> |
