To extend the XML document with elements not specified by the schema, the <any> element is used.
The <any> Element:
To make EXTENSIBLE documents, i.e., to allow documents to contain additional elements that are not declared in the main XML schema, the <any> and <anyAttribute> elements are used.
Example:
<xs:element name="student"> <xs:complextype> <xs:sequence> <xs:element name="firstname" type="xs:string"></xs:element> <xs:element name="lastname" type="xs:string"></xs:element> <xs:any minOccurs="0"></xs:any> </xs:sequence> </xs:complextype> </xs:element> |
Explanation:
In the above example, we are displaying a fragment from an XML schema called “friend.xsd”, declaring the “student” element. The content of “student” with any element can be extended after <lastname> by using the <any> element.
sibling.xsd:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3spoint.com" xmlns="https://www.w3spoint.com" elementFormDefault="qualified"> <xs:element name="sibling"> <xs:complextype> <xs:sequence> <xs:element name="siblingname" type="xs:string" maxOccurs="unbounded"></xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:schema> |
Explanation:
In the above example, we are extending the “student” element with a “sibling” element, even if the author of the schema above never declared any “sibling” element.
Myfriend.xml:
<?xml version="1.0" encoding="UTF-8"?> <students xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.microsoft.com family.xsd https://www.w3spoint.com sibling.xsd"> <student> <firstname>Tom</firstname> <lastname>Jonas</lastname> <sibling> <siblingname>Jack</siblingname> </sibling> </student> <student> <firstname>John</firstname> <lastname>Davis</lastname> </student> </students> |
Explanation:
In the above example, we are creating the “Myfriend.xml” file to use the components from two different schemas; “friend.xsd” and “sibling.xsd”. Now, since the schema “friend.xsd” allows us to extend the “student” element with an optional element after the “lastname” element, the XML file Myfriend.xml is valid.