องค์ประกอบแอตทริบิวต์ XML Schema
❮ การอ้างอิงสคีมา XML ที่สมบูรณ์
ความหมายและการใช้งาน
องค์ประกอบแอตทริบิวต์กำหนดแอตทริบิวต์
ข้อมูลองค์ประกอบ
- องค์ประกอบหลัก: attributeGroup, schema, 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>
ตัวอย่างข้างต้นระบุว่าแอตทริบิวต์ "code" มีข้อจำกัด ค่าเดียวที่ยอมรับได้คืออักษรตัวพิมพ์ใหญ่สองตัวจาก 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 ที่สมบูรณ์