Element ograniczenia schematu XML
❮ Pełne odniesienie do schematu XML
Definicja i użycie
Element ograniczenia definiuje ograniczenia dotyczące definicji simpleType, simpleContent lub complexContent.
Informacje o elemencie
- Elementy nadrzędne: simpleType, simpleContent, complexContent
Składnia
<restriction
id=ID
base=QName
any attributes
>
Content for simpleType:
(annotation?,(simpleType?,(minExclusive|minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*))
Content for simpleContent:
(annotation?,(simpleType?,(minExclusive |minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?,
((attribute|attributeGroup)*,anyAttribute?))
Content for complexContent:
(annotation?,(group|all|choice|sequence)?,
((attribute|attributeGroup)*,anyAttribute?))
</restriction>
(Znak ? deklaruje, że element może wystąpić zero lub jeden raz wewnątrz elementu ograniczenia)
Attribute | Description |
---|---|
id | Optional. Specifies a unique ID for the element |
base |
Required. Specifies the name of a built-in data type, simpleType element, or complexType element defined in this schema or another schema |
any attributes | Optional. Specifies any other attributes with non-schema namespace |
Przykład 1
Ten przykład definiuje element o nazwie „wiek” z ograniczeniem. Wartość wieku NIE może być mniejsza niż 0 ani większa niż 100:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Przykład 2
Ten przykład definiuje również element o nazwie „inicjały”. Element „inicjały” jest prostym typem z ograniczeniem. Jedyną dopuszczalną wartością są TRZY WIELKIE LUB WIELKIE litery od a do z:
<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>
Przykład 3
Ten przykład definiuje element o nazwie „hasło”. Element „password” jest prostym typem z ograniczeniem. Wartość musi składać się z co najmniej pięciu znaków i maksymalnie ośmiu znaków:
<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>
Przykład 4
Ten przykład przedstawia definicję typu złożonego przy użyciu ograniczenia. Typ złożony „Norwegian_customer” pochodzi od ogólnego typu złożonego klienta, a jego element kraju jest ustalony na „Norwegia”:
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Norwegian_customer">
<xs:complexContent>
<xs:restriction base="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string" fixed="Norway"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
❮ Pełne odniesienie do schematu XML