Elemento de restricción del esquema XML
❮ Referencia completa del esquema XML
Definición y uso
El elemento de restricción define restricciones en una definición simpleType, simpleContent o complexContent.
Información del elemento
- Elementos principales: simpleType, simpleContent, complexContent
Sintaxis
<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>
(El signo ? declara que el elemento puede aparecer cero o una vez dentro del elemento de restricción)
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 |
Ejemplo 1
Este ejemplo define un elemento llamado "edad" con una restricción. El valor de la edad NO puede ser inferior a 0 ni superior a 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>
Ejemplo 2
Este ejemplo también define un elemento llamado "iniciales". El elemento "iniciales" es un tipo simple con una restricción. El único valor aceptable es TRES de las letras MINÚSCULAS O MAYÚSCULAS de la a a la 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>
Ejemplo 3
Este ejemplo define un elemento llamado "contraseña". El elemento "contraseña" es un tipo simple con una restricción. El valor debe tener un mínimo de cinco caracteres y un máximo de ocho caracteres:
<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>
Ejemplo 4
Este ejemplo muestra una definición de tipo complejo usando restricción. El tipo complejo "Norwegian_customer" se deriva de un tipo complejo de cliente general y su elemento de país se fija en "Noruega":
<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>
❮ Referencia completa del esquema XML