องค์ประกอบ <any> ของ XSD
- หน้าก่อน ตัวชนะ XSD
- หน้าต่อไป XSD <anyAttribute>
<any> ส่วนตัวนี้ทำให้เรามีความสามารถที่จะขยาย XML โดยการใช้องค์ประกอบที่ยังไม่ถูกกำหนดโดย schema!
<any> ส่วนตัว
<any> ส่วนตัวนี้ทำให้เรามีความสามารถที่จะขยาย XML โดยการใช้องค์ประกอบที่ยังไม่ถูกกำหนดโดย schema!
ตัวอย่างดังกล่าวนี้เป็นส่วนหนึ่งที่อ้างอิงมาจาก XML schema ที่มีชื่อว่า "family.xsd" มันแสดงให้เห็นการประกาศสำหรับประกาย "person" ด้วยการใช้สิ่งที่เรียกว่า <any> พวกเราสามารถขยายเนื้อหาของ "person" ด้วยองค์ประกอบใดก็ได้ (หลังจาก <lastname>)
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>
现在,我们希望使用 "children" 元素来扩展 "person" 元素。这此种情况下我们就可以这么做,即使以上这个 schema 的作者没有声明任何 "children" 元素。
请看这个 schema 文件,名为 "children.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:element name="children"> <xs:complexType> <xs:sequence> <xs:element name="childname" type="xs:string" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
下面这个 XML 文件(名为 "Myfamily.xml"),使用了来自两个不同的 schema 中的成分,"family.xsd" 和 "children.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 children.xsd"> <person> <firstname>David</firstname> <lastname>Smith</lastname> <children> <childname>mike</childname> </children> </person> <person> <firstname>Tony</firstname> <lastname>Smith</lastname> </person> </persons>
ไฟล์ XML ที่อยู่ด้านบนนี้เป็นไฟล์ที่มีความถูกต้อง เนื่องจาก schema "family.xsd" อนุญาตให้เราขยายองค์ประกอบ "person" ด้วยองค์ประกอบที่เป็นเครื่องเลือกที่อยู่หลังจากองค์ประกอบ "lastname"
<any> และ <anyAttribute> สามารถใช้เพื่อทำเอกสารที่สามารถขยายได้ พวกนี้ทำให้เอกสารสามารถรับรององค์ประกอบที่ไม่ได้ประกาศใน XML schema หลัก
- หน้าก่อน ตัวชนะ XSD
- หน้าต่อไป XSD <anyAttribute>