Elemento attributo XML Schema
❮ Riferimento completo allo schema XML
Definizione e utilizzo
L'elemento attributo definisce un attributo.
Informazioni sull'elemento
- Elementi principali: attributeGroup, schema, complexType, restrizione (sia simpleContent che complexContent), estensione (sia simpleContent che complexContent)
Sintassi
<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>
(Il segno ? dichiara che l'elemento può verificarsi zero o una volta all'interno dell'elemento attributo)
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 |
Esempio 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>
L'esempio sopra indica che l'attributo "code" ha una restrizione. L'unico valore accettabile sono due delle lettere maiuscole dalla a alla z.
Esempio 2
Per dichiarare un attributo utilizzando una definizione di attributo esistente all'interno di un tipo complesso, utilizzare l'attributo 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>
Esempio 3
Gli attributi possono avere un valore predefinito OPPURE un valore fisso specificato. Un valore predefinito viene assegnato automaticamente all'attributo quando non viene specificato nessun altro valore. Nell'esempio seguente il valore predefinito è "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
Un valore fisso viene assegnato automaticamente anche all'attributo quando non viene specificato nessun altro valore. Ma a differenza dei valori predefiniti; se si specifica un valore diverso da quello fisso, il documento è considerato non valido. Nell'esempio seguente il valore fisso è "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Esempio 4
Tutti gli attributi sono facoltativi per impostazione predefinita. Per specificare esplicitamente che l'attributo è facoltativo, utilizzare l'attributo "use":
<xs:attribute name="lang" type="xs:string" use="optional"/>
Per rendere obbligatorio un attributo:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Riferimento completo allo schema XML