XML Schema attribuut Element
❮ Volledige XML-schemareferentie
Definitie en gebruik
Het attribuutelement definieert een attribuut.
Elementinformatie
- Bovenliggende elementen: attributeGroup, schema, complexType, restrictie (zowel simpleContent als complexContent), extension (zowel simpleContent als complexContent)
Syntaxis
<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>
(Het ?-teken geeft aan dat het element nul of één keer binnen het attribuutelement kan voorkomen)
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 |
voorbeeld 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>
Het bovenstaande voorbeeld geeft aan dat het kenmerk "code" een beperking heeft. De enige acceptabele waarde is twee van de hoofdletters van a tot z.
Voorbeeld 2
Om een attribuut te declareren met behulp van een bestaande attribuutdefinitie binnen een complex type, gebruikt u het ref attribuut:
<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>
Voorbeeld 3
Attributen kunnen een standaardwaarde OF een vaste waarde hebben. Er wordt automatisch een standaardwaarde aan het kenmerk toegewezen als er geen andere waarde is opgegeven. In het volgende voorbeeld is de standaardwaarde "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
Er wordt ook automatisch een vaste waarde aan het attribuut toegewezen als er geen andere waarde is opgegeven. Maar in tegenstelling tot standaardwaarden; als u een andere waarde opgeeft dan de vaste waarde, wordt het document als ongeldig beschouwd. In het volgende voorbeeld is de vaste waarde "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Voorbeeld 4
Alle attributen zijn standaard optioneel. Om expliciet te specificeren dat het attribuut optioneel is, gebruikt u het "use" attribuut:
<xs:attribute name="lang" type="xs:string" use="optional"/>
Een attribuut verplicht maken:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Volledige XML-schemareferentie