XSD - <any>
<any> 元素用於擴充套件 XSD 功能。它用於透過一個不在架構中定義的元素擴充套件一個在某一 XSD 容器定義的 complexType 元素。
考慮一個示例 - person.xsd 已定義 person complexType 元素。address.xsd 已定義 address complexType 元素。
person.xsd
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "https://tutorialspoint.tw"
xmlns = "https://tutorialspoint.tw"
elementFormDefault = "qualified">
<xs:element name = "person">
<xs:complexType >
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "lastname" type = "xs:string"/>
<xs:element name = "nickname" type = "xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
address.xsd
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "https://tutorialspoint.tw"
xmlns = "https://tutorialspoint.tw"
elementFormDefault = "qualified">
<xs:element name = "address">
<xs:complexType>
<xs:sequence>
<xs:element name = "houseNumber" type = "xs:string"/>
<xs:element name = "street" type = "xs:string"/>
<xs:element name = "state" type = "xs:string"/>
<xs:element name = "zipcode" type = "xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
如果我們想要在 XML 中定義帶地址的人, 則以下宣告將無效。
person.xml
<?xml version = "1.0"?>
<class xmlns = "https://tutorialspoint.tw"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "https://tutorialspoint.tw person.xsd
https://tutorialspoint.tw address.xsd">
<person>
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</lastname>
<address>
<houseNumber>101</firstname>
<street>Sector-1,Patiala</lastname>
<state>Punjab</lastname>
<zipcode>301202<zipcode>
</address>
</person>
</class>
使用 <xs:any>
為了驗證上面的 person.xml,將 <xs:any> 新增到 person.xsd 中的 person 元素。
person.xsd
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "https://tutorialspoint.tw"
xmlns = "https://tutorialspoint.tw"
elementFormDefault = "qualified">
<xs:element name = "person">
<xs:complexType >
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "lastname" type = "xs:string"/>
<xs:element name = "nickname" type = "xs:string"/>
<xs:any minOccurs = "0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
現在,person.xml 將針對 person.xsd 和 address.xsd 進行驗證。
xsd_complex_types.htm
廣告