Element atrybutu schematu XML
❮ Pełne odniesienie do schematu XML
Definicja i użycie
Element atrybutu definiuje atrybut.
Informacje o elemencie
- Elementy nadrzędne: atrybutGroup, schemat, complexType, ograniczenie (zarówno simpleContent, jak i complexContent), rozszerzenie (zarówno simpleContent, jak i complexContent)
Składnia
<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>
(Znak ? deklaruje, że element może wystąpić zero lub jeden raz wewnątrz elementu atrybutu)
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 |
Przykład 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>
Powyższy przykład wskazuje, że atrybut „code” ma ograniczenie. Jedyną dopuszczalną wartością są dwie wielkie litery od a do z.
Przykład 2
Aby zadeklarować atrybut przy użyciu istniejącej definicji atrybutu w typie złożonym, użyj atrybutu 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>
Przykład 3
Atrybuty mogą mieć wartość domyślną LUB określoną wartość stałą. Wartość domyślna jest automatycznie przypisywana do atrybutu, gdy nie określono żadnej innej wartości. W poniższym przykładzie domyślną wartością jest „EN”:
<xs:attribute name="lang" type="xs:string" default="EN"/>
Stała wartość jest również automatycznie przypisywana do atrybutu, gdy nie określono żadnej innej wartości. Ale w przeciwieństwie do wartości domyślnych; jeśli podasz inną wartość niż ustalona, dokument zostanie uznany za nieważny. W poniższym przykładzie stała wartość to „EN”:
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Przykład 4
Wszystkie atrybuty są domyślnie opcjonalne. Aby jawnie określić, że atrybut jest opcjonalny, użyj atrybutu „use”:
<xs:attribute name="lang" type="xs:string" use="optional"/>
Aby atrybut był wymagany:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Pełne odniesienie do schematu XML