Elemento de atributo de esquema XML
❮ Referência Completa do Esquema XML
Definição e uso
O elemento attribute define um atributo.
Informações do elemento
- Elementos pai: attributeGroup, esquema, complexType, restrição (simplesContent e complexContent), extensão (simplesContent e complexContent)
Sintaxe
<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>
(O sinal ? declara que o elemento pode ocorrer zero ou uma vez dentro do elemento 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 |
Exemplo 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>
O exemplo acima indica que o atributo "code" tem uma restrição. O único valor aceitável são duas letras maiúsculas de a a z.
Exemplo 2
Para declarar um atributo usando uma definição de atributo existente em um tipo complexo, use o 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>
Exemplo 3
Os atributos podem ter um valor padrão OU um valor fixo especificado. Um valor padrão é atribuído automaticamente ao atributo quando nenhum outro valor é especificado. No exemplo a seguir, o valor padrão é "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
Um valor fixo também é atribuído automaticamente ao atributo quando nenhum outro valor é especificado. Mas ao contrário dos valores padrão; se você especificar um valor diferente do fixo, o documento será considerado inválido. No exemplo a seguir, o valor fixo é "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Exemplo 4
Todos os atributos são opcionais por padrão. Para especificar explicitamente que o atributo é opcional, use o atributo "use":
<xs:attribute name="lang" type="xs:string" use="optional"/>
Para tornar um atributo obrigatório:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Referência Completa do Esquema XML