定義簡易元素
<xs:element name="xxx" type="yyy"/>
簡易元素
- xxx 指的是元素名稱
- yyy 指元素類型
- 元素類型
- xs:string
- xs:decimal
- xs:integer
- xs:boolean
- xs:date
- xs:time
例子
<xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> <lastname>Smith</lastname> <age>28</age> <dateborn>1980-03-27</dateborn>
- 默認值和固定值
- 默認值:
<xs:element name="color" type="xs:string" default="red"/>
- 固定值:無法分配
<xs:element name="color" type="xs:string" fixed="red"/>
- 默認值:
屬性
<xs:attribute name="xxx" type="yyy"/>
- 簡易元素無法擁有屬性。假如某個元素擁有屬性,它就會被當作某種複合類型。但是屬性本身總是作爲簡易類型被聲明的。
- xxx 指的是屬性名稱
- yyy 指屬性類型
- 常用的屬性類型
- xs:string
- xs:decimal
- xs:integer
- xs:boolean
- xs:date
- xs:time
例子
<xs:attribute name="lang" type="xs:string"/> <lastname lang="EN">Smith</lastname>
- 默認值和固定值
- 默認值:屬性是可選的
<xs:attribute name="lang" type="xs:string" default="EN"/>
- 設置爲必選: use=”required”
<xs:attribute name="lang" type="xs:string" use="required"/>
- 固定值:無法分配
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
- 默認值:屬性是可選的
限定
限定數字大小
- 限制單獨屬性的例子:age 的值不能低於 0 或者高於 120。
<xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> </xs:element>
- 限制多個1屬性的例子:age 的值不能低於 0 或者高於 120。
<xs:element name="age" type="ageType"/> <xs:simpleType name="ageType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType>
限定值是枚舉
- 限制單獨屬性的例子:可接受的值只有:Audi, Golf, BMW。
<xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element>
- 限制多個2屬性的例子:在這種情況下,類型 “carType” 可被其他元素使用,因爲它不是 “car” 元素的組成部分。可接受的值只有:Audi, Golf, BMW。
<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>
限定值正則表達式規定的
- 限制單獨屬性的例子:可接受的值只能是小寫字母。
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element>
- 限制多個屬性的例子:同上個案例
限定空白字符
保存空白字符,不做任何更改
- 例子:關鍵字:
<xs:whiteSpace value="preserve"/>
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </xs:element>
- 例子:關鍵字:
不保留空白字符,所有的空白字符都將移除
- 例子:關鍵字:
<xs:whiteSpace value="replace"/>
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="replace"/> </xs:restriction> </xs:simpleType> </xs:element>
- 例子:關鍵字:
保留多個連續的空格,縮減爲一個,其餘的移除
- 例子:關鍵字:
<xs:whiteSpace value="collapse"/>
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse"/> </xs:restriction> </xs:simpleType> </xs:element>
- 例子:關鍵字:
對長度的限定
- 值必須精確到 8 個字符
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:element>
- 值最小爲 5 個字符,最大爲 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>
數據類型的限定
限定 | 描述 |
---|---|
enumeration | 定義可接受值的一個列表 |
fractionDigits | 定義所允許的最大的小數位數。必須大於等於0。 |
length | 定義所允許的字符或者列表項目的精確數目。必須大於或等於0。 |
maxExclusive | 定義數值的上限。所允許的值必須小於此值。 |
maxInclusive | 定義數值的上限。所允許的值必須小於或等於此值。 |
maxLength | 定義所允許的字符或者列表項目的最大數目。必須大於或等於0。 |
minExclusive | 定義數值的下限。所允許的值必需大於此值。 |
minInclusive | 定義數值的下限。所允許的值必需大於或等於此值。 |
minLength | 定義所允許的字符或者列表項目的最小數目。必須大於或等於0。 |
pattern | 定義可接受的字符的精確序列。 |
totalDigits | 定義所允許的阿拉伯數字的精確位數。必須大於0。 |
whiteSpace | 定義空白字符(換行、回車、空格以及製表符)的處理方式。 |