XSD <anyAttribute>องค์ประกอบ
องค์ประกอบ <anyAttribute> ช่วยให้เราสามารถขยายเอกสาร XML ด้วยแอตทริบิวต์ที่ไม่ได้ระบุโดยสคีมา!
<anyAttribute> องค์ประกอบ
องค์ประกอบ <anyAttribute> ช่วยให้เราสามารถขยายเอกสาร XML ด้วยแอตทริบิวต์ที่ไม่ได้ระบุโดยสคีมา
ตัวอย่างต่อไปนี้เป็นส่วนย่อยจาก XML schema ที่เรียกว่า "family.xsd" มันแสดงการประกาศสำหรับองค์ประกอบ "บุคคล" โดยใช้องค์ประกอบ <anyAttribute> เราสามารถเพิ่มแอตทริบิวต์จำนวนเท่าใดก็ได้ให้กับองค์ประกอบ "บุคคล":
<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>
ตอนนี้ เราต้องการขยายองค์ประกอบ "บุคคล" ด้วยแอตทริบิวต์ "สีตา" ในกรณีนี้ เราสามารถทำได้ แม้ว่าผู้เขียนสคีมาด้านบนจะไม่เคยประกาศแอตทริบิวต์ "สีตา" ใดๆ ก็ตาม
ดูไฟล์สคีมานี้ชื่อ "attribute.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">
<xs:attribute name="eyecolor">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="blue|brown|green|grey"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>
ไฟล์ XML ด้านล่าง (เรียกว่า "Myfamily.xml") ใช้ส่วนประกอบจากสคีมาสองแบบที่แตกต่างกัน "family.xsd" และ "attribute.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
https://www.w3schools.com attribute.xsd">
<person eyecolor="green">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person eyecolor="blue">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
ไฟล์ XML ด้านบนนี้ใช้ได้เนื่องจากสคีมา "family.xsd" ช่วยให้เราเพิ่มแอตทริบิวต์ให้กับองค์ประกอบ "บุคคล" ได้
องค์ประกอบ <any> และ <anyAttribute> ใช้เพื่อสร้างเอกสาร EXTENSIBLE! อนุญาตให้เอกสารมีองค์ประกอบเพิ่มเติมที่ไม่ได้ประกาศใน XML schema หลัก