Элемент ограничения схемы XML
❮ Полный справочник по XML-схеме
Определение и использование
Элемент ограничения определяет ограничения для определения simpleType, simpleContent или complexContent.
Информация об элементе
- Родительские элементы: simpleType, simpleContent, complexContent
Синтаксис
<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>
(Знак ? указывает, что элемент может встречаться ноль или один раз внутри элемента ограничения)
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 |
Пример 1
В этом примере определяется элемент с именем «возраст» с ограничением. Значение возраста НЕ может быть меньше 0 или больше 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>
Пример 2
В этом примере также определяется элемент под названием «инициалы». Элемент «initials» — это простой тип с ограничением. Единственным допустимым значением является ТРИ СТРОЧНЫХ ИЛИ ПРОПИСНЫХ буквы от a до 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>
Пример 3
В этом примере определяется элемент с именем «пароль». Элемент «пароль» — это простой тип с ограничением. Значение должно состоять минимум из пяти и максимум из восьми символов:
<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>
Пример 4
В этом примере показано определение сложного типа с использованием ограничения. Сложный тип «Norwegian_customer» является производным от общего сложного типа клиента, а его элемент страны фиксируется на «Норвегия»:
<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>
❮ Полный справочник по XML-схеме