Elemen atribut Skema XML
Referensi Skema XML Lengkap
Definisi dan Penggunaan
Elemen atribut mendefinisikan atribut.
Informasi Elemen
- Elemen induk: atributGroup, skema, complexType, pembatasan (baik simpleContent dan complexContent), ekstensi (baik simpleContent dan complexContent)
Sintaksis
<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>
(Tanda ? menyatakan bahwa elemen dapat muncul nol atau satu kali di dalam elemen atribut)
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 |
Contoh 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>
Contoh di atas menunjukkan bahwa atribut "kode" memiliki batasan. Satu-satunya nilai yang dapat diterima adalah dua huruf besar dari a sampai z.
Contoh 2
Untuk mendeklarasikan atribut menggunakan definisi atribut yang ada dalam tipe kompleks, gunakan 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>
Contoh 3
Atribut dapat memiliki nilai default ATAU nilai tetap yang ditentukan. Nilai default secara otomatis ditetapkan ke atribut ketika tidak ada nilai lain yang ditentukan. Dalam contoh berikut, nilai defaultnya adalah "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
Nilai tetap juga secara otomatis ditetapkan ke atribut ketika tidak ada nilai lain yang ditentukan. Tapi tidak seperti nilai default; jika Anda menentukan nilai lain selain nilai tetap, dokumen dianggap tidak valid. Dalam contoh berikut, nilai tetapnya adalah "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Contoh 4
Semua atribut bersifat opsional secara default. Untuk secara eksplisit menentukan bahwa atribut tersebut opsional, gunakan atribut "gunakan":
<xs:attribute name="lang" type="xs:string" use="optional"/>
Untuk membuat atribut yang diperlukan:
<xs:attribute name="lang" type="xs:string" use="required"/>
Referensi Skema XML Lengkap