XSD (xml Schema Definition)

Xml Schema的用途

1. 定義一個Xml文檔中都有什么元素

2. 定義一個Xml文檔中都會有什么屬性

3. 定義某個節(jié)點的都有什么樣的子節(jié)點,可以有多少個子節(jié)點,子節(jié)點出現(xiàn)的順序

4. 定義元素或者屬性的數(shù)據(jù)類型

5. 定義元素或者屬性的默認(rèn)值或者固定值

Xml Schema的根元素:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 表示數(shù)據(jù)類型等定義來自w3

targetNamespace="http://www.w3schools.com" 表示文檔中要定義的元素來自什么命名空間

xmlns="http://www.w3schools.com"表示此文檔的默認(rèn)命名空間是什么

elementFormDefault="qualified"> 表示要求xml文檔的每一個元素都要有命名空間指定

……定義主體部分……

</xs:schema>

如何定義一個簡單元素

<xs:element 此處表示要定義一個元素

name=”color” 表示要定義元素的名稱

type=”xs:string” 表示要定義元素的數(shù)據(jù)類型

default=”red” 表示定義元素的默認(rèn)值

fixed=”red”/> 表示要定義元素的固定值,此元素只可以取“red”值

以上定義了一個簡單元素,元素實例:<color>red</color>

如何定義一個屬性

<xs:attribute

     name=”birthday” 表示要定義屬性的名字

     type=”xs:date” 表示要定義屬性的數(shù)據(jù)類型

     default=”2001-01-11” 表示要定義屬性的默認(rèn)值

     fixed=”2001-01-11” 表示要定義屬性的固定值

     use=”required”/> 表示此屬性是否是必須指定的,即如果不指定就不符合Schema,默認(rèn)沒有use=”required”屬性表示屬性可有可無

如何定義元素或者屬性值的限制

1.最大值最小值限制

<xs:element name="age">

<xs:simpleType>

<xs:restriction base="xs:integer">

<xs:minInclusive value="0"/> 大于等于0,<xs: minExclusive>表示最小值但是不包括指定值

<xs:maxInclusive value="120"/> 小于等于120,<xs: maxExclusive>

</xs:restriction>

</xs:simpleType>

</xs:element>

2.枚舉限制,指只能在指定的幾個值中取值

     <xs:element name="car" type="carType"/>

<xs:simpleType name="carType">

<xs:restriction base="xs:string">

<xs:enumeration value="Audi"/>

<xs:enumeration value="Golf"/>

<xs:enumeration value="BMW"/>

</xs:restriction>

</xs:simpleType>

3.模式(pattern)限制 ,指字符串的格式必須滿足制定的匹配模式

例子

說明

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

表示只能在小寫字母中取一個值

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
表示必須是三個大寫字母

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
表示必須是三個字母,可以是大寫或小寫的

<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

表示必須是xyz中的一個

<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
表示數(shù)字的范圍是0-99999

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
表示必須是0或者多個小寫字符組成的序列

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
表示必須是多個字母。

<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
表示是male或者female中的一個

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
表示必須是8個字母數(shù)字字符

4.字符串長度的限制

<xs:element name="password">

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:length value="8"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

長度必須是8。

<xs:element name="password">

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:minLength value="5"/>

<xs:maxLength value="8"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

表示長度在5-8之間

6. 對于空白字符的限制

示例

說明

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

保留原樣,表示xml處理器不會移除或者替換任何空白字符

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
指回車,換行,Tab都會被替換成空格處理

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
去掉多于一個空格,和html中處理方式相同

如何定義復(fù)雜類型

復(fù)雜類型是指定義元素中包含屬性或者子元素的類型

1. 定義只包含子元素的復(fù)雜類型

<xs:element name="person">

<xs:complexType>

<xs:sequence>

  <xs:element name="firstname" type="xs:string"/>

  <xs:element name="lastname" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:element>

2. 定義只包含屬性的復(fù)雜類型

<xs:element name="product" type="prodtype"/>

<xs:complexType name="prodtype">

<xs:attribute name="prodid" type="xs:positiveInteger"/>

</xs:complexType>

3. 定義只包含內(nèi)容的復(fù)雜類型

<xs:element name="shoesize" type="shoetype"/>

<xs:complexType name="shoetype">

<xs:simpleContent>

<xs:extension base="xs:integer">

  <xs:attribute name="country" type="xs:string" />

</xs:extension>

</xs:simpleContent>

</xs:complexType>

4. 定義包含內(nèi)容和子元素混合的復(fù)雜類型

<xs:element name="letter">

<xs:complexType mixed="true">

<xs:sequence>

  <xs:element name="name" type="xs:string"/>

  <xs:element name="orderid" type="xs:positiveInteger"/>

  <xs:element name="shipdate" type="xs:date"/>

</xs:sequence>

</xs:complexType>

</xs:element>

以上定義對應(yīng)的Xml

<letter>

Dear Mr.<name>John Smith</name>.

Your order <orderid>1032</orderid>

will be shipped on <shipdate>2001-07-13</shipdate>.

</letter>

5. 定義包含屬性和子元素的復(fù)雜類型

使用指示器

在Xsd中的指示器包括

1. 順序指示器

1) All

指示子元素可以以任何順序出現(xiàn),并且每一個元素都必須出現(xiàn)一次

<xs:element name="person">

<xs:complexType>

<xs:all>

  <xs:element name="firstname" type="xs:string"/>

  <xs:element name="lastname" type="xs:string"/>

</xs:all>

</xs:complexType>

</xs:element>

2) Choice

指示子元素中可以出現(xiàn)一個或者另一個

<xs:element name="person">

<xs:complexType>

<xs:choice>

  <xs:element name="employee" type="employee"/>

  <xs:element name="member" type="member"/>

</xs:choice>

</xs:complexType>

</xs:element>

3) Sequence

指示子元素必須按照順序出現(xiàn)

<xs:element name="person">

<xs:complexType>

<xs:sequence>

  <xs:element name="firstname" type="xs:string"/>

  <xs:element name="lastname" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:element>

2. 出現(xiàn)次數(shù)指示器minOccurs,maxOccurs

<xs:element name="person">

<xs:complexType>

<xs:sequence>

  <xs:element name="full_name" type="xs:string"/>

  <xs:element name="child_name" type="xs:string"

  maxOccurs="10" minOccurs="0"/>

</xs:sequence>

</xs:complexType>

</xs:element>

3. 組指示器(group Indicators)

用來定義相關(guān)的一組元素

<xs:group name="persongroup">

<xs:sequence>

<xs:element name="firstname" type="xs:string"/>

<xs:element name="lastname" type="xs:string"/>

<xs:element name="birthday" type="xs:date"/>

</xs:sequence>

</xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">

<xs:sequence>

<xs:group ref="persongroup"/>

<xs:element name="country" type="xs:string"/>

</xs:sequence>

</xs:complexType>

用來定義一組相關(guān)的屬性

<xs:attributeGroup name="personattrgroup">

<xs:attribute name="firstname" type="xs:string"/>

<xs:attribute name="lastname" type="xs:string"/>

<xs:attribute name="birthday" type="xs:date"/>

</xs:attributeGroup>

<xs:element name="person">

<xs:complexType>

<xs:attributeGroup ref="personattrgroup"/>

</xs:complexType>

</xs:element>

Any關(guān)鍵字

表示可以有任意元素

<xs:element name="person">

<xs:complexType>

<xs:sequence>

  <xs:element name="firstname" type="xs:string"/>

  <xs:element name="lastname" type="xs:string"/>

  <xs:any minOccurs="0"/>

</xs:sequence>

</xs:complexType>

</xs:element>

anyAttribute關(guān)鍵字

<xs:element name="person">

<xs:complexType>

<xs:sequence>

  <xs:element name="firstname" type="xs:string"/>

  <xs:element name="lastname" type="xs:string"/>

</xs:sequence>

<xs:anyAttribute/>

</xs:complexType>

</xs:element>

substitutionGroup關(guān)鍵字

表示某一個元素和另一個替代元素定義相同

<xs:element name="name" type="xs:string"/>

<xs:element name="navn" substitutionGroup="name"/>

<xs:complexType name="custinfo">

<xs:sequence>

<xs:element ref="name"/>

</xs:sequence>

</xs:complexType><xs:element name="customer" type="custinfo"/>

<xs:element name="kunde" substitutionGroup="customer"/>

文中的例子都來自w3school.

---------XSD工具

XSD工具位于:C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin目錄下。XSD工具有很多功能,如下:

1、將XDR 轉(zhuǎn)換 XSD

使用精簡 XML 數(shù)據(jù)架構(gòu)文件生成 XML 架構(gòu)。 XDR 為早期基于 XML 的架構(gòu)格式。

2、XML轉(zhuǎn)換XSD

使用 XML 文件生成 XML 架構(gòu)。

3、XSD轉(zhuǎn)換DataSet

使用 XSD 架構(gòu)文件生成公共語言運(yùn)行庫 DataSet 類。 生成的類為規(guī)則 XML 數(shù)據(jù)提供復(fù)雜對象模型。

4、XSD轉(zhuǎn)換 類

使用 XSD 架構(gòu)文件生成運(yùn)行庫類。 生成的類可以與 System.Xml.Serialization.XmlSerializer 一起使用,來讀寫遵循該架構(gòu)的 XML 代碼。

5、類 轉(zhuǎn)換XSD

使用運(yùn)行庫程序集文件中的一個或多個類型生成 XML 架構(gòu)。 生成的架構(gòu)定義了 System.Xml.Serialization.XmlSerializer 使用的 XML 格式。

XSD語法格式如下:

Xsd file.xdr [/outputdir:directory][/parameters:file.xml]

xsd file.xml [/outputdir:directory] [/parameters:file.xml]

xsd file.xsd {/classes | /dataset} [/element:element]

         [/enableLinqDataSet] [/language:language]

                      [/namespace:namespace] [/outputdir:directory] [URI:uri]

                      [/parameters:file.xml]

Xsd {file.dll | file.exe} [/outputdir:directory] [/type:typename [...]][/parameters:file.xml]

參數(shù)說明

緊跟在xsd命令后面的參數(shù)是指定的要轉(zhuǎn)換的輸入文件。 該輸入文件必須將擴(kuò)展名指定為下列之一:.xdr、.xml、.xsd、.dll 或 .exe。

1、 如果指定一個 XDR 架構(gòu)文件(.xdr 擴(kuò)展名),則 Xsd.exe 將 XDR 架構(gòu)轉(zhuǎn)換為 XSD 架構(gòu)。 輸出文件與 XDR 架構(gòu)同名,但擴(kuò)展名為 .xsd。

2、 如果指定一個 XML 文件(.xml 擴(kuò)展名),則 Xsd.exe 從文件中的數(shù)據(jù)推導(dǎo)出架構(gòu)并產(chǎn)生一個 XSD 架構(gòu)。 輸出文件與 XML 文件同名,但擴(kuò)展名為 .xsd。

3、 如果指定一個 XML 架構(gòu)文件(.xsd 擴(kuò)展名),則 Xsd.exe 將為對應(yīng)于 XML 架構(gòu)的運(yùn)行庫對象生成源代碼。

4、如果指定一個運(yùn)行庫程序集文件(.exe 或 .dll 擴(kuò)展名),則 Xsd.exe 為該程序集中的一個或多個類型生成架構(gòu)。 可以使用 /type 選項來指定為其生成架構(gòu)的類型。 輸出架構(gòu)被命名為 schema0.xsd、schema1.xsd,依此類推。 僅當(dāng)給定類型使用 XMLRoot 自定義屬性指定命名空間時,Xsd.exe 才生成多個架構(gòu)。

常規(guī)選項說明:

/h[elp]

顯示該工具的命令語法和選項。

/o[utputdir]:directory

指定輸出文件的目錄。 此參數(shù)只能出現(xiàn)一次。 默認(rèn)為當(dāng)前目錄。

/?

顯示該工具的命令語法和選項。

/P[arameters]: file.xml

從指定的 .xml 文件讀取各種操作模式的選項。 縮寫形式為“/p:”。 有關(guān)更多信息,請參見下面的“備注”部分。

XSD 文件選項

必須為 xsd 文件僅指定下列選項中的一個。

選項說明

/c[lasses]

生成與指定架構(gòu)相對應(yīng)的類。 若要將 XML 數(shù)據(jù)讀入對象,請使用 System.Xml.Serialization.XmlSerializer.Deserializer 方法。

/d[ataset]

生成一個從 DataSet 派生的類,該類與指定的架構(gòu)相對應(yīng)。 若要將 XML 數(shù)據(jù)讀入派生類,請使用 System.Data.DataSet.ReadXml 方法。

還可以為 .xsd 文件指定下列任何選項。

選項說明 :

/e[lement]:element

指定架構(gòu)中要為其生成代碼的元素。 默認(rèn)情況下,鍵入所有元素。 可以多次指定該參數(shù)。

/enableDataBinding

在所有生成的類型上實現(xiàn) INotifyPropertyChanged 接口以啟用數(shù)據(jù)綁定。 縮寫形式為“/edb”。

/enableLinqDataSet

(縮寫形式:/eld。) 指定可使用 查詢的生成的數(shù)據(jù)集。 此選項在同時指定 /dataset 選項的情況下使用。 有關(guān)更多信息,請參見 LINQ to DataSet Overview 和 Querying Typed DataSets。 有關(guān)使用 LINQ 的常規(guī)信息,請參見Language-Integrated Query (LINQ)。

/f[ields]

生成字段,而不是生成屬性。 默認(rèn)情況下生成屬性。

/l[anguage]:language

指定要使用的編程語言。 從 CS(默認(rèn)情況下為 C#)、VB (Visual Basic)、JS (JScript) 或 VJS (Visual J#) 中進(jìn)行選擇。 也可指定實現(xiàn) System.CodeDom.Compiler.CodeDomProvider 的類的完全限定名

/n[amespace]:namespace

為生成的類型指定運(yùn)行庫命名空間。 默認(rèn)命名空間為 Schemas。

/nologo

取消顯示版權(quán)標(biāo)志。

/order

在所有粒子成員上生成顯式順序標(biāo)識符。

/o[ut]: directoryName

指定要放置文件的輸出目錄。默認(rèn)為當(dāng)前目錄。

/u[ri]:uri

為架構(gòu)中要為其生成代碼的元素指定 URI。 該 URI(如果存在)應(yīng)用于使用 /element 選項指定的所有元素。

DLL 和 EXE 文件選項

選項說明:

/t[ype]:typename

指定要為其創(chuàng)建架構(gòu)的類型的名稱。 可以指定多個類型參數(shù)。 如果 typename 不指定一個命名空間,則 Xsd.exe 將程序集中的所有類型與指定類型相匹配。 如果 typename 指定一個命名空間,則僅匹配那個類型。 如果 typename 以星號字符 (*) 結(jié)尾,則此工具匹配所有以 * 前的字符串開頭的類型。 如果省略 /type 選項,則 Xsd.exe 為程序集中的所有類型生成架構(gòu)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. XML簡介 以下內(nèi)容來自于http://www.w3school.com.cn/xml 基本知識 XML 和...
    WebSSO閱讀 2,092評論 1 7
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • xml (extensible markup language)1.xml 是可擴(kuò)展標(biāo)記語言2.xml是一種標(biāo)記語...
    帥哥_刷哥閱讀 775評論 0 1
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,517評論 2 7
  • 簡單類型(simpleType) 1,簡單元素:指只能包含文本內(nèi)容,不能夠包含子元素,也沒有屬性的元素。 2,屬性...
    持續(xù)進(jìn)步者閱讀 1,059評論 0 2

友情鏈接更多精彩內(nèi)容