Elemento de atributo de esquema XML
❮ Referencia completa del esquema XML
Definición y uso
El elemento de atributo define un atributo.
Información del elemento
- Elementos principales: atributoGroup, esquema, complexType, restricción (tanto simpleContent como complexContent), extensión (tanto simpleContent como complexContent)
Sintaxis
<attribute
default=string
fixed=string
form=qualified|unqualified
id=ID
name=NCName
ref=QName
type=QName
use=optional|prohibited|required
any attributes
>
(annotation?,(simpleType?))
</attribute>
(El signo ? declara que el elemento puede aparecer cero o una vez dentro del elemento de atributo)
Attribute | Description |
---|---|
default | Optional. Specifies a default value for the attribute. Default and fixed attributes cannot both be present |
fixed | Optional. Specifies a fixed value for the attribute. Default and fixed attributes cannot both be present |
form | Optional. Specifies the form for the attribute. The default value is
the value of the attributeFormDefault attribute of the element
containing the attribute. Can be set to one of the following:
|
id | Optional. Specifies a unique ID for the element |
name | Optional. Specifies the name of the attribute. Name and ref attributes cannot both be present |
ref | Optional. Specifies a reference to a named attribute. Name and ref attributes cannot both be present. If ref is present, simpleType element, form, and type cannot be present |
type | Optional. Specifies a built-in data type or a simple type. The type attribute can only be present when the content does not contain a simpleType element |
use | Optional. Specifies how the attribute is used. Can be one
of the following values:
|
any attributes | Optional. Specifies any other attributes with non-schema namespace |
Ejemplo 1
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
El ejemplo anterior indica que el atributo "código" tiene una restricción. El único valor aceptable son dos de las letras mayúsculas de la a a la z.
Ejemplo 2
Para declarar un atributo usando una definición de atributo existente dentro de un tipo complejo, use el atributo ref:
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="someComplexType">
<xs:attribute ref="code"/>
</xs:complexType>
Ejemplo 3
Los atributos pueden tener un valor predeterminado O un valor fijo especificado. Un valor predeterminado se asigna automáticamente al atributo cuando no se especifica ningún otro valor. En el siguiente ejemplo, el valor predeterminado es "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
También se asigna automáticamente un valor fijo al atributo cuando no se especifica ningún otro valor. Pero a diferencia de los valores predeterminados; si especifica otro valor que el fijo, el documento se considera inválido. En el siguiente ejemplo, el valor fijo es "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Ejemplo 4
Todos los atributos son opcionales por defecto. Para especificar explícitamente que el atributo es opcional, use el atributo "use":
<xs:attribute name="lang" type="xs:string" use="optional"/>
Para hacer que un atributo sea obligatorio:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Referencia completa del esquema XML