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.com"
xmlns = "https://tutorialspoint.com"
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.com"
xmlns = "https://tutorialspoint.com"
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.com"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "https://tutorialspoint.com person.xsd
https://tutorialspoint.com 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.com"
xmlns = "https://tutorialspoint.com"
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
广告