XSD - 限制
restriction 元素用於定義 XML 元素可以接受的值。
語法
<xs:restriction base = "element-type"> restrictions </xs:restriction>
| base | 要應用限制的元素的型別。例如, <xs:restriction base = "xs:integer"> 指定此限制特定於型別為 int 的元素。 |
| restriction | restriction 通常是一系列要應用於元素值的條件。在本例中,我們對分數設定了一個限制,即分數應在 0 到 100 的範圍內,這兩個值都包含在內。 <xs:minInclusive value = "0"/> <xs:maxInclusive value = "100"/> |
示例
值限制。
條件 - 分數應在 0 到 100 的範圍內。
<xs:element name = "marks">
<xs:simpleType>
<xs:restriction base = "xs:integer">
<xs:minInclusive value = "0"/>
<xs:maxInclusive value = "100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
值集限制。
條件 - 等級只能是 A、B 或 C。
<xs:element name = "grades">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:enumeration value = "A"/>
<xs:enumeration value = "B"/>
<xs:enumeration value = "C"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
使用正則表示式的限制。
條件 - 名字只能包含字母。
<xs:element name = "firstname">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:pattern value = "[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
限制類型
| 序號 | 限制 & 描述 |
|---|---|
| 1 | enumeration 定義一個可接受值的列表。 |
| 2 | fractionDigits 定義允許的小數位數的最大值(零或更多)。 |
| 3 | length 根據字串的字元數或列表中的專案數定義長度(零或更多)。 |
| 4 | maxExclusive 定義數值的上限,不包括此數字。 |
| 5 | maxInclusive 定義數值的上限,包括此數字。 |
| 6 | maxLength 根據字串的字元數或列表中的專案數定義最大長度(零或更多)。 |
| 7 | minExclusive 定義數值的下限,不包括此數字。 |
| 8 | minInclusive 定義數值的下限,包括此數字。 |
| 9 | minLength 根據字串的字元數或列表中的專案數定義最小長度(零或更多)。 |
| 10 | pattern 定義可接受的模式確定的字元的精確序列。 |
| 11 | totalDigits 定義數字中允許的精確位數(始終大於零)。 |
| 12 | whiteSpace 定義如何處理空白字元(換行符、製表符、空格和回車符)。 |
xsd_simple_types.htm
廣告