Prvek atributu schématu XML
❮ Vyplňte odkaz na schéma XML
Definice a použití
Element atribut definuje atribut.
Informace o prvku
- Nadřazené prvky: atributGroup, schema, complexType, omezení (jak simpleContent, tak complexContent), rozšíření (jak simpleContent, tak 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>
(Znak ? deklaruje, že prvek se může uvnitř atributového prvku vyskytovat nula nebo jednou)
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 |
Příklad 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>
Výše uvedený příklad naznačuje, že atribut "code" má omezení. Jedinou přijatelnou hodnotou jsou dvě velká písmena od a do z.
Příklad 2
Chcete-li deklarovat atribut pomocí existující definice atributu v rámci komplexního typu, použijte atribut 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>
Příklad 3
Atributy mohou mít buď výchozí hodnotu, NEBO zadanou pevnou hodnotu. Pokud není zadána žádná jiná hodnota, je atributu automaticky přiřazena výchozí hodnota. V následujícím příkladu je výchozí hodnota "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
Pevná hodnota je také automaticky přiřazena k atributu, pokud není zadána žádná jiná hodnota. Ale na rozdíl od výchozích hodnot; pokud zadáte jinou hodnotu než pevnou, bude dokument považován za neplatný. V následujícím příkladu je pevná hodnota "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Příklad 4
Všechny atributy jsou ve výchozím nastavení volitelné. Chcete-li explicitně určit, že atribut je volitelný, použijte atribut „use“:
<xs:attribute name="lang" type="xs:string" use="optional"/>
Chcete-li, aby byl atribut vyžadován:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Vyplňte odkaz na schéma XML