xsd文件规则和语法
生活随笔
收集整理的這篇文章主要介紹了
xsd文件规则和语法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.簡介
XSD即XML結構定義, XML Schemas Definition。其本身就是用xml描述的, 且遵循xml語法規則。一份XML schema文件描述了XML文檔的結構.
基本規則:
? .必須以 XML 聲明開頭
? .必須擁有唯一的根元素
? .標簽必須與結束標簽相匹配
? .元素對大小寫敏感
? .所有的元素都必須關閉
? .所有的元素都必須正確地嵌套
? .必須對特殊字符使用實體
??
<!– xsd –>
<?xml version=”1.0”?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”
targetNamespace=”http://www.w3school.com.cn”
xmlns=”http://www.w3school.com.cn”
elementFormDefault=”qualified”>
<xs:element name=”note”>
? <xs:complexType>
? ? <xs:sequence>
<xs:element name=”to” type=”xs:string”/>
<xs:element name=”from” type=”xs:string”/>
<xs:element name=”heading” type=”xs:string”/>
<xs:element name=”body” type=”xs:string”/>
? ? </xs:sequence>
? </xs:complexType>
</xs:element>
</xs:schema>
<!– 對應的xml一個例子 –>
<?xml version=”1.0”?>
<note>
? <to>George</to>
? <from>John</from>
? <heading>Reminder</heading>
? <body>Don’t forget the meeting!</body>
</note>
2.定義一個元素
<xs:element name=”xxx” type=”yyy”/>
<xs:element表示定義一個元素
xxx 指元素的名稱,yyy 指元素的數據類型。XML Schema 擁有很多內建的數據類型
3.定義一個屬性
<xs:attribute name=”xxx” type=”yyy”/>
xs:attribute 表示定義一個屬性
xxx 指屬性名稱,yyy 則規定屬性的數據類型。XML Schema 擁有很多內建的數據類型
4.自定義簡單類型(用于復合元素)
<xs:simpleType name=”sinceType”>
? <xs:restriction base=”xs:int”>
? ? ? <xs:minInclusive value=”0”/>
? ? ? <xs:maxInclusive value=”100”/>
? </xs:restriction>
</xs:simpleType>
<xs:element name=”goal” type=”sinceType”/>
xs:simpleType 表示接下來是簡單類型的定義. 而使用此類型的元素,一般被稱作復合元素
5.自定義結構體(用于復合元素)
<xs:complexType name=”combinationType”>
? <xs:element name=”Name” type=”xs:string” />
? <xs:element name=”Age” type=”xs:string” />
</xs:complexType>
<xs:element name=”person” type=”combinationType”/>
xs:complexType 表示接下來是結構體的定義. 而使用此類型的元素,一般被稱作復合元素.
6.混合內容的自定義類型元素
<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>
xs:complexType mixed=”true” 指明了letter里邊的內容是混合內容, 其可以是如下形式:
? <letter>
? Dear Mr.<name>John Smith</name>.
? Your order <orderid>1032</orderid>
? will be shipped on <shipdate>2001-07-13</shipdate>.
? </letter>
7.約束條件
<xs:element name=”age”>
<xs:simpleType>
? <xs:restriction base=”xs:int”>
? ? ? <xs:minInclusive value=”1”/>
? ? ? <xs:maxInclusive value=”100”/>
? </xs:restriction>
</xs:simpleType>
</xs:element>
xs:restriction 表示接下來是對元素/屬性的約束
8.可選的和必需出現(屬性)
<xs:attribute name=”lang” type=”xs:string” use=”required”/>
use 表示出現的要求,required/optional/prohibited, 分別是必須出現/可選/不出現
9.出現次數(元素)
<xs:element name=”child_name” type=”xs:string” maxOccurs=”10” minOccurs=”0”/>
minOccurs 表示最小出現的次數,并由數值/unbounded 來指定
maxOccurs 表示最大出現的次數,并由數值/unbounded 來指定
10.默認值和固定值(元素/屬性)
<xs:element name=”pen” type=”xs:string” default=”minipen”/>
<xs:attribute name=”color” type=”xs:string” fixed=”red”/>
default 指定默認值, fixed 指定該元素值固定
11.數據類型限制(元素/屬性)
<xs:element name=”Name”>
<xs:simpleType>
<xs:restriction base=”xs:string”>
? ? ? <xs:minLength value=”2” />
? ? ? ?<xs:maxLength value=”4” />
?</xs:restriction>
</xs:simpleType>
</xs:element>
xs:restriction 表示對元素/屬性的限制.
限制種類有以下類別:
? .length ? ? ? ?// 定義所允許的字符或者列表項目的精確數目。必須大于或等于0。
? .minLength ? ?// 定義所允許的字符或者列表項目的最小數目。必須大于或等于0。
? .maxLength ? ?// 定義所允許的字符或者列表項目的最大數目。必須大于或等于0。
??
? .minInclusive // 定義數值的下限。所允許的值必需大于或等于此值。?
? .maxInclusive // 定義數值的上限。所允許的值必須小于或等于此值。
??
? .minExclusive // 定義數值的下限。所允許的值必需大于此值。
? .maxExclusive // 定義數值的上限。所允許的值必須小于此值。
??
? .enumeration // 定義可接受值的一個列表
? .pattern ? ?// 定義可接受的字符的精確序列(規則表達式)。
? .fractionDigits // 定義所允許的最大的小數位數。必須大于等于0
? .totalDigits // 定義所允許的阿拉伯數字的精確位數。必須大于0。
? .whiteSpace ? ?// 定義空白字符(換行、回車、空格以及制表符)的處理方式。
??
12.空白字符的限制
<xs:element name=”address”>
<xs:simpleType>
<xs:restriction base=”xs:string”>
? <xs:whiteSpace value=”preserve”/>
</xs:restriction>
</xs:simpleType>
</xs:element>?
xs:whiteSpace 指明對空白字符的限制, 取值有preserve/collapse/replace, 一次是保留/只保留字符中間必須的空格,且多個合并為一個/全部去掉
13.子元素順序
<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>
子元素順尋有:
? .xs:all ? ? ? ? // 順序不限, 每個子元素只準出現一次
? .xs:choice ? ? ?// 順序不限, 每個子元素出現次數不限
? .xs:sequence ? ?// 順序固定, 每個子元素出現次數不限
14.元素組
<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>
xs:group 指明定義一個元素組或者引用一個已經定義的元素組. 這樣可以減少不必要的重復書寫.
15.屬性組
<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>
xs:attributeGroup 指明定義一個屬性組或者引用一個已經定義的屬性組. 這樣可以減少不必要的重復書寫.
16.不限定子元素
<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=”unbounded”/>
? </xs:sequence>
</xs:complexType>
</xs:element>
xs:any 指明可以包含一個不限定子元素. 如以下xml也是符合xsd的:
? <person>
? ? ? <firstname>David</firstname>
? ? ? <lastname>Smith</lastname>
? ? ? <children>
? ? ? ? ? <childname>mike</childname>
? ? ? </children>
? </person>
17.不限定屬性
<xs:element name=”person”>
<xs:complexType>
? <xs:sequence>
? ? <xs:element name=”firstname” type=”xs:string”/>
? ? <xs:element name=”lastname” type=”xs:string”/>
? ? <xs:anyAttribute minOccurs=”unbounded”/>
? </xs:sequence>
</xs:complexType>
</xs:element>
anyAttribute 指明可以包含一個不限定屬性
18.替換元素
<xs:element name=”name” type=”xs:string”/>
<xs:element name=”navn” substitutionGroup=”name”/>
substitutionGroup 指明本元素可以替換的元素. 可替換元素的類型必須和主元素相同,同時兩者必須是全局元素。
如以下xml片段都是合法的:
? <customer>
? ? <name>John Smith</name>
? </customer>
? <customer>
? ? <navn>John Smith</navn>
? </customer>
19.阻止被替換
<xs:element name=”name” type=”xs:string” block=”substitution”/>
<xs:element name=”navn” substitutionGroup=”name”/>
block 指明阻止的內容,這里為被替換. 可替換元素的類型必須和主元素相同,同時兩者必須是全局元素。
本例就阻止用任何來替換name的聲明,以下xml片段是錯誤的:
? <customer>
? ? <navn>John Smith</navn>
? </customer>
20.全局元素/本地元素
全局元素指 “schema” 元素的直接子元素
本地元素(Local elements)指嵌套在其他元素中的元素
21.xsd內置數據類型
xs:byte ? ? ? ? ? ? ? ? // 字節型,8位二進制位長
xs:unsignedByte ? ? ? ? // 無符號字節型
xs:short ? ? ? ? ? ? ? ?// 短整型, 16位二進制位長
xs:unsignedShort ? ? ? ?// 無符號短整型
xs:integer ? ? ? ? ? ? ?// 整型, 32位二進制位長
xs:positiveInteger ? ? ?// 只有正值的整型
xs:nonPositiveInteger ? // 只有非正值的整型
xs:negativeInteger ? ? ?// 只有負值的整型
xs:nonNegativeInteger ? // 只有非負值的整型
xs:int ? ? ? ? ? ? ? ? ?// 整型, 32位二進制位長
xs:long ? ? ? ? ? ? ? ? // 長整型, 64位二進制位長
xs:unsignedLong ? ? ? ? // 無符號長整型
xs:float ? ? ? ? ? ? ? ?// 浮點數, 32位二進制位長
xs:double ? ? ? ? ? ? ? // 雙精度, 64位二進制位長
xs:string ? ? ? ? ? ? ? // 字符串
xs:boolean ? ? ? ? ? ? ?// 合法的布爾值是 true、false、1(表示 true) 以及 0(表示 false)
xs:hexBinary ? ? ? ? ? ?// 十六進制編碼的二進制數據
xs:base64Binary ? ? ? ? // Base64 編碼的二進制數據
xs:date ? ? ? ? ? ? ? ? // 日期
xs:decimal ? ? ? ? ? ? ?// 貨幣類型
xs:anyURI ? ? ? ? ? ? ? // 假如某個 URI 含有空格,請用替換它們
22.調用其他程序/xml外部類型
<xs:notation name=”gif” public=”image/gif” system=”view.exe”/>
xs:notation 表示此元素非xml類型, 可指定解析或執行的程序.
xml文檔中的內容類似如下:
<image type=”gif”>demopic.gif</image>
23.注釋
<xs:annotation>
? <xs:documentation xml:lang=”zh”>
? 這是一份用于企業雇員信息描述的模式文檔
? </xs:documentation>
</xs:annotation>
“<!–”和”–>”仍可以使用,但是為了方便其他讀者和應用程序來理解模式文檔,XML Schema提供了三個元素來為模式提供注解:
? .xs:annotation ? ? ?// 作為documentation/appinfo的父元素
? .xs:documentation ? // 放置適合人閱讀的信息
? .xs:appinfo ? ? ? ? // 工具、樣式表和其他應用程序提供信息
24.命名空間
名稱空間聲明的一般形式為
? 第一部分是一個關鍵字xmlns:
? 第二部分是名稱空間的前綴(標識符)
? 第三部分是一個等號
? 第四部分是雙引號
? 第五部分是名稱空間標識URI
需要注意的是,名稱空間的前綴不能為xml,因為在XML中這個字符串是保留作特殊用途的。
<?xml version=”1.0”?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”>
</xs:schema>
??
25.源命名空間
在Schema中的定義和聲明也可以引用其他的命名空間,我們可以把這種命名空間取名為源命名空間(source namespaces)。每一個Schema可以有多個源命名空間。?
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
</xs:schema>
26.目標命名空間
每一個Schema可以有且只有一個目標命名空間。在一個給定的Schema中,每一個名稱都是屬于一個特定的名字空間的, 同時可以由targetNamespace來命名
<?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema” targetNamespace=“http://www.test.com/ns/ns_test“>
</xsd:schema>
xsd文件中定義了一個targetNameSpace后,其內部定義的元素,屬性,類型等都屬于該targetNameSpace
其自身或外部xsd文件使用這些元素,屬性等都必須從定義的targetNameSpace中找
27.引入xsd命名空間
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
<import namespace=”http://www.PartnerStore.com/PartsCatalog” schemaLocation=”http://www.ProductStandards.org/repository/alpha.xsd”/>?
</schema>
import 必須指明使用xmlns:xsi,因為import/schemaLocation都是在xsi(xml schema instance)中定義.
XSD即XML結構定義, XML Schemas Definition。其本身就是用xml描述的, 且遵循xml語法規則。一份XML schema文件描述了XML文檔的結構.
基本規則:
? .必須以 XML 聲明開頭
? .必須擁有唯一的根元素
? .標簽必須與結束標簽相匹配
? .元素對大小寫敏感
? .所有的元素都必須關閉
? .所有的元素都必須正確地嵌套
? .必須對特殊字符使用實體
??
<!– xsd –>
<?xml version=”1.0”?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”
targetNamespace=”http://www.w3school.com.cn”
xmlns=”http://www.w3school.com.cn”
elementFormDefault=”qualified”>
<xs:element name=”note”>
? <xs:complexType>
? ? <xs:sequence>
<xs:element name=”to” type=”xs:string”/>
<xs:element name=”from” type=”xs:string”/>
<xs:element name=”heading” type=”xs:string”/>
<xs:element name=”body” type=”xs:string”/>
? ? </xs:sequence>
? </xs:complexType>
</xs:element>
</xs:schema>
<!– 對應的xml一個例子 –>
<?xml version=”1.0”?>
<note>
? <to>George</to>
? <from>John</from>
? <heading>Reminder</heading>
? <body>Don’t forget the meeting!</body>
</note>
2.定義一個元素
<xs:element name=”xxx” type=”yyy”/>
<xs:element表示定義一個元素
xxx 指元素的名稱,yyy 指元素的數據類型。XML Schema 擁有很多內建的數據類型
3.定義一個屬性
<xs:attribute name=”xxx” type=”yyy”/>
xs:attribute 表示定義一個屬性
xxx 指屬性名稱,yyy 則規定屬性的數據類型。XML Schema 擁有很多內建的數據類型
4.自定義簡單類型(用于復合元素)
<xs:simpleType name=”sinceType”>
? <xs:restriction base=”xs:int”>
? ? ? <xs:minInclusive value=”0”/>
? ? ? <xs:maxInclusive value=”100”/>
? </xs:restriction>
</xs:simpleType>
<xs:element name=”goal” type=”sinceType”/>
xs:simpleType 表示接下來是簡單類型的定義. 而使用此類型的元素,一般被稱作復合元素
5.自定義結構體(用于復合元素)
<xs:complexType name=”combinationType”>
? <xs:element name=”Name” type=”xs:string” />
? <xs:element name=”Age” type=”xs:string” />
</xs:complexType>
<xs:element name=”person” type=”combinationType”/>
xs:complexType 表示接下來是結構體的定義. 而使用此類型的元素,一般被稱作復合元素.
6.混合內容的自定義類型元素
<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>
xs:complexType mixed=”true” 指明了letter里邊的內容是混合內容, 其可以是如下形式:
? <letter>
? Dear Mr.<name>John Smith</name>.
? Your order <orderid>1032</orderid>
? will be shipped on <shipdate>2001-07-13</shipdate>.
? </letter>
7.約束條件
<xs:element name=”age”>
<xs:simpleType>
? <xs:restriction base=”xs:int”>
? ? ? <xs:minInclusive value=”1”/>
? ? ? <xs:maxInclusive value=”100”/>
? </xs:restriction>
</xs:simpleType>
</xs:element>
xs:restriction 表示接下來是對元素/屬性的約束
8.可選的和必需出現(屬性)
<xs:attribute name=”lang” type=”xs:string” use=”required”/>
use 表示出現的要求,required/optional/prohibited, 分別是必須出現/可選/不出現
9.出現次數(元素)
<xs:element name=”child_name” type=”xs:string” maxOccurs=”10” minOccurs=”0”/>
minOccurs 表示最小出現的次數,并由數值/unbounded 來指定
maxOccurs 表示最大出現的次數,并由數值/unbounded 來指定
10.默認值和固定值(元素/屬性)
<xs:element name=”pen” type=”xs:string” default=”minipen”/>
<xs:attribute name=”color” type=”xs:string” fixed=”red”/>
default 指定默認值, fixed 指定該元素值固定
11.數據類型限制(元素/屬性)
<xs:element name=”Name”>
<xs:simpleType>
<xs:restriction base=”xs:string”>
? ? ? <xs:minLength value=”2” />
? ? ? ?<xs:maxLength value=”4” />
?</xs:restriction>
</xs:simpleType>
</xs:element>
xs:restriction 表示對元素/屬性的限制.
限制種類有以下類別:
? .length ? ? ? ?// 定義所允許的字符或者列表項目的精確數目。必須大于或等于0。
? .minLength ? ?// 定義所允許的字符或者列表項目的最小數目。必須大于或等于0。
? .maxLength ? ?// 定義所允許的字符或者列表項目的最大數目。必須大于或等于0。
??
? .minInclusive // 定義數值的下限。所允許的值必需大于或等于此值。?
? .maxInclusive // 定義數值的上限。所允許的值必須小于或等于此值。
??
? .minExclusive // 定義數值的下限。所允許的值必需大于此值。
? .maxExclusive // 定義數值的上限。所允許的值必須小于此值。
??
? .enumeration // 定義可接受值的一個列表
? .pattern ? ?// 定義可接受的字符的精確序列(規則表達式)。
? .fractionDigits // 定義所允許的最大的小數位數。必須大于等于0
? .totalDigits // 定義所允許的阿拉伯數字的精確位數。必須大于0。
? .whiteSpace ? ?// 定義空白字符(換行、回車、空格以及制表符)的處理方式。
??
12.空白字符的限制
<xs:element name=”address”>
<xs:simpleType>
<xs:restriction base=”xs:string”>
? <xs:whiteSpace value=”preserve”/>
</xs:restriction>
</xs:simpleType>
</xs:element>?
xs:whiteSpace 指明對空白字符的限制, 取值有preserve/collapse/replace, 一次是保留/只保留字符中間必須的空格,且多個合并為一個/全部去掉
13.子元素順序
<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>
子元素順尋有:
? .xs:all ? ? ? ? // 順序不限, 每個子元素只準出現一次
? .xs:choice ? ? ?// 順序不限, 每個子元素出現次數不限
? .xs:sequence ? ?// 順序固定, 每個子元素出現次數不限
14.元素組
<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>
xs:group 指明定義一個元素組或者引用一個已經定義的元素組. 這樣可以減少不必要的重復書寫.
15.屬性組
<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>
xs:attributeGroup 指明定義一個屬性組或者引用一個已經定義的屬性組. 這樣可以減少不必要的重復書寫.
16.不限定子元素
<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=”unbounded”/>
? </xs:sequence>
</xs:complexType>
</xs:element>
xs:any 指明可以包含一個不限定子元素. 如以下xml也是符合xsd的:
? <person>
? ? ? <firstname>David</firstname>
? ? ? <lastname>Smith</lastname>
? ? ? <children>
? ? ? ? ? <childname>mike</childname>
? ? ? </children>
? </person>
17.不限定屬性
<xs:element name=”person”>
<xs:complexType>
? <xs:sequence>
? ? <xs:element name=”firstname” type=”xs:string”/>
? ? <xs:element name=”lastname” type=”xs:string”/>
? ? <xs:anyAttribute minOccurs=”unbounded”/>
? </xs:sequence>
</xs:complexType>
</xs:element>
anyAttribute 指明可以包含一個不限定屬性
18.替換元素
<xs:element name=”name” type=”xs:string”/>
<xs:element name=”navn” substitutionGroup=”name”/>
substitutionGroup 指明本元素可以替換的元素. 可替換元素的類型必須和主元素相同,同時兩者必須是全局元素。
如以下xml片段都是合法的:
? <customer>
? ? <name>John Smith</name>
? </customer>
? <customer>
? ? <navn>John Smith</navn>
? </customer>
19.阻止被替換
<xs:element name=”name” type=”xs:string” block=”substitution”/>
<xs:element name=”navn” substitutionGroup=”name”/>
block 指明阻止的內容,這里為被替換. 可替換元素的類型必須和主元素相同,同時兩者必須是全局元素。
本例就阻止用任何來替換name的聲明,以下xml片段是錯誤的:
? <customer>
? ? <navn>John Smith</navn>
? </customer>
20.全局元素/本地元素
全局元素指 “schema” 元素的直接子元素
本地元素(Local elements)指嵌套在其他元素中的元素
21.xsd內置數據類型
xs:byte ? ? ? ? ? ? ? ? // 字節型,8位二進制位長
xs:unsignedByte ? ? ? ? // 無符號字節型
xs:short ? ? ? ? ? ? ? ?// 短整型, 16位二進制位長
xs:unsignedShort ? ? ? ?// 無符號短整型
xs:integer ? ? ? ? ? ? ?// 整型, 32位二進制位長
xs:positiveInteger ? ? ?// 只有正值的整型
xs:nonPositiveInteger ? // 只有非正值的整型
xs:negativeInteger ? ? ?// 只有負值的整型
xs:nonNegativeInteger ? // 只有非負值的整型
xs:int ? ? ? ? ? ? ? ? ?// 整型, 32位二進制位長
xs:long ? ? ? ? ? ? ? ? // 長整型, 64位二進制位長
xs:unsignedLong ? ? ? ? // 無符號長整型
xs:float ? ? ? ? ? ? ? ?// 浮點數, 32位二進制位長
xs:double ? ? ? ? ? ? ? // 雙精度, 64位二進制位長
xs:string ? ? ? ? ? ? ? // 字符串
xs:boolean ? ? ? ? ? ? ?// 合法的布爾值是 true、false、1(表示 true) 以及 0(表示 false)
xs:hexBinary ? ? ? ? ? ?// 十六進制編碼的二進制數據
xs:base64Binary ? ? ? ? // Base64 編碼的二進制數據
xs:date ? ? ? ? ? ? ? ? // 日期
xs:decimal ? ? ? ? ? ? ?// 貨幣類型
xs:anyURI ? ? ? ? ? ? ? // 假如某個 URI 含有空格,請用替換它們
22.調用其他程序/xml外部類型
<xs:notation name=”gif” public=”image/gif” system=”view.exe”/>
xs:notation 表示此元素非xml類型, 可指定解析或執行的程序.
xml文檔中的內容類似如下:
<image type=”gif”>demopic.gif</image>
23.注釋
<xs:annotation>
? <xs:documentation xml:lang=”zh”>
? 這是一份用于企業雇員信息描述的模式文檔
? </xs:documentation>
</xs:annotation>
“<!–”和”–>”仍可以使用,但是為了方便其他讀者和應用程序來理解模式文檔,XML Schema提供了三個元素來為模式提供注解:
? .xs:annotation ? ? ?// 作為documentation/appinfo的父元素
? .xs:documentation ? // 放置適合人閱讀的信息
? .xs:appinfo ? ? ? ? // 工具、樣式表和其他應用程序提供信息
24.命名空間
名稱空間聲明的一般形式為
? 第一部分是一個關鍵字xmlns:
? 第二部分是名稱空間的前綴(標識符)
? 第三部分是一個等號
? 第四部分是雙引號
? 第五部分是名稱空間標識URI
需要注意的是,名稱空間的前綴不能為xml,因為在XML中這個字符串是保留作特殊用途的。
<?xml version=”1.0”?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”>
</xs:schema>
??
25.源命名空間
在Schema中的定義和聲明也可以引用其他的命名空間,我們可以把這種命名空間取名為源命名空間(source namespaces)。每一個Schema可以有多個源命名空間。?
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
</xs:schema>
26.目標命名空間
每一個Schema可以有且只有一個目標命名空間。在一個給定的Schema中,每一個名稱都是屬于一個特定的名字空間的, 同時可以由targetNamespace來命名
<?xml version=”1.0”>
<xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema” targetNamespace=“http://www.test.com/ns/ns_test“>
</xsd:schema>
xsd文件中定義了一個targetNameSpace后,其內部定義的元素,屬性,類型等都屬于該targetNameSpace
其自身或外部xsd文件使用這些元素,屬性等都必須從定義的targetNameSpace中找
27.引入xsd命名空間
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
<import namespace=”http://www.PartnerStore.com/PartsCatalog” schemaLocation=”http://www.ProductStandards.org/repository/alpha.xsd”/>?
</schema>
import 必須指明使用xmlns:xsi,因為import/schemaLocation都是在xsi(xml schema instance)中定義.
總結
以上是生活随笔為你收集整理的xsd文件规则和语法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原来搞单片机也可以面向对象
- 下一篇: C语言,把指针按地上摩擦,爽