Элемент атрибута схемы XML
❮ Полный справочник по XML-схеме
Определение и использование
Элемент атрибута определяет атрибут.
Информация об элементе
- Родительские элементы: attributeGroup, схема, complexType, ограничение (как simpleContent, так и complexContent), расширение (как simpleContent, так и complexContent)
Синтаксис
<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>
(Знак ? указывает, что элемент может встречаться ноль или один раз внутри элемента атрибута)
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 |
Пример 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>
В приведенном выше примере показано, что атрибут «код» имеет ограничение. Единственным приемлемым значением являются две заглавные буквы от a до z.
Пример 2
Чтобы объявить атрибут, используя существующее определение атрибута в сложном типе, используйте атрибут 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>
Пример 3
Атрибуты могут иметь либо значение по умолчанию, либо указанное фиксированное значение. Значение по умолчанию автоматически присваивается атрибуту, если не указано другое значение. В следующем примере значением по умолчанию является "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
Фиксированное значение также автоматически присваивается атрибуту, если не указано другое значение. Но в отличие от значений по умолчанию; если указать значение, отличное от фиксированного, документ считается недействительным. В следующем примере фиксированным значением является «EN»:
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Пример 4
Все атрибуты являются необязательными по умолчанию. Чтобы явно указать, что атрибут является необязательным, используйте атрибут «use»:
<xs:attribute name="lang" type="xs:string" use="optional"/>
Чтобы сделать атрибут обязательным:
<xs:attribute name="lang" type="xs:string" use="required"/>
❮ Полный справочник по XML-схеме