องค์ประกอบ <anyAttribute> ของ XSD
- หน้าก่อนหน้า XSD <any>
- หน้าต่อไป การแทนที่องค์ประกอบ XSD
องค์ประกอบ <anyAttribute> มีความสามารถในการขยาย XML ได้ด้วยคุณสมบัติที่ไม่ถูกกำหนดโดย schema!
องค์ประกอบ <anyAttribute>
องค์ประกอบ <anyAttribute> มีความสามารถในการขยาย XML ได้ด้วยคุณสมบัติที่ไม่ถูกกำหนดโดย schema!
ตัวอย่างดังกล่าวคือส่วนหนึ่งของ XML schema ที่มีชื่อว่า "family.xsd" มันแสดงให้เห็นถึงการประกาศสำหรับองค์ประกอบ "person" โดยใช้องค์ประกอบ <anyAttribute> สามารถเพิ่มคุณสมบัติที่ไม่ถูกกำหนดโดย schema ให้กับองค์ประกอบ "person" ได้มากมาย:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:anyAttribute/> </xs:complexType> </xs:element>
ตอนนี้ เราต้องการขยาย element "person" ด้วย attribute "gender" โดยไม่ว่าผู้เขียน schema จะประกาศ attribute "gender" อย่างไรก็ตาม。
ดูไฟล์ schema นี้ ชื่อว่า "attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.codew3c.com" xmlns="http://www.codew3c.com" elementFormDefault="qualified"> <xs:attribute name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:schema>
ด้านนี้นี้เป็น XML (ชื่อว่า "Myfamily.xml") ที่ใช้ส่วนประกอบจาก schema ต่าง ๆ คือ "family.xsd" และ "attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.com family.xsd http://www.codew3c.com attribute.xsd"> <person gender="female"> <firstname>Jane</firstname> <lastname>Smith</lastname> </person> <person gender="male"> <firstname>David</firstname> <lastname>Smith</lastname> </person> </persons>
ไฟล์ XML ที่มีความถูกต้องนี้เนื่องจาก schema "family.xsd" อนุญาตให้เราเพิ่มคุณสมบัติให้กับองค์ประกอบ "person"
<any> และ <anyAttribute> สามารถใช้ทำเอกสารที่สามารถขยายได้ นั่นทำให้เอกสารมีความสามารถรับความสัมพันธ์ขององค์ประกอบที่ไม่ได้ประกาศใน XML schema หลัก
- หน้าก่อนหน้า XSD <any>
- หน้าต่อไป การแทนที่องค์ประกอบ XSD