XSD <any> 元素

สมาชิก <any> ทำให้เรามีความสามารถในการขยาย XML document ด้วยสมาชิกที่ยังไม่ได้กำหนดโดย schema!

สมาชิก <any>

สมาชิก <any> ทำให้เรามีความสามารถในการขยาย XML document ด้วยสมาชิกที่ยังไม่ได้กำหนดโดย schema!

ตัวอย่างนี้เป็นส่วนที่เอามาจาก XML schema ที่มีชื่อว่า "family.xsd" มันแสดงการประกาศสำหรับสมาชิก "person" ด้วยการใช้สมาชิก <any> เราสามารถขยายเนื้อหาของ "person" ด้วยสมาชิกที่ไม่ได้กำหนดไว้ใน schema:

<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 ไฟล์ดังกล่าวนี้เป็นไฟล์ XML ที่มีความถูกต้อง นี่เนื่องมาจาก schema "family.xsd" ที่อนุญาตให้เราขยาย element "person" ผ่าน element ตัวเลือกหลังจาก element "lastname"

<any> 和 <anyAttribute> 均可使用於製作可擴展的文檔!它們使文檔有能力包含未在主 XML schema 中聲明過的附加元素。