XML- Schemaattribut Element
❮ Vollständige XML-Schema-Referenz
Definition und Verwendung
Das Attributelement definiert ein Attribut.
Elementinformationen
- Übergeordnete Elemente: attributeGroup, schema, complexType, Beschränkung (sowohl simpleContent als auch complexContent), Erweiterung (sowohl simpleContent als auch complexContent)
Syntax
<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>
(Das ?-Zeichen gibt an, dass das Element null oder einmal innerhalb des Attributelements vorkommen kann.)
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 |
Beispiel 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>
Das obige Beispiel zeigt, dass das Attribut "Code" eine Einschränkung hat. Der einzig akzeptable Wert sind zwei der Großbuchstaben von a bis z.
Beispiel 2
Um ein Attribut mithilfe einer vorhandenen Attributdefinition innerhalb eines komplexen Typs zu deklarieren, verwenden Sie das ref-Attribut:
<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>
Beispiel 3
Attribute können entweder einen Standardwert ODER einen festen Wert haben. Dem Attribut wird automatisch ein Standardwert zugewiesen, wenn kein anderer Wert angegeben wird. Im folgenden Beispiel ist der Standardwert "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
Auch wenn kein anderer Wert angegeben wird, wird dem Attribut automatisch ein fester Wert zugewiesen. Aber im Gegensatz zu Standardwerten; Wenn Sie einen anderen als den festen Wert angeben, wird das Dokument als ungültig betrachtet. Im folgenden Beispiel ist der feste Wert "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Beispiel 4
Alle Attribute sind standardmäßig optional. Um explizit anzugeben, dass das Attribut optional ist, verwenden Sie das Attribut „use“:
<xs:attribute name="lang" type="xs:string" use="optional"/>
So machen Sie ein Attribut erforderlich:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Vollständige XML-Schema-Referenz