Latest web development tutorials

XML Schema any element

> <Any> element by element gives us the ability to expand is not specified by the schema of an XML document!


h2> <any> element

<Any> element by element gives us the ability to expand is not specified by the schema of an XML document!

The following example is quoted fragment from XML schema called "family.xsd" in. It shows a declaration for the "person" element. By using the <any> element, we can use any element (in the <lastname>) After extended "person" elements:

<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>

Now, we want to use the "children" element to extend the "person" element. Under such circumstances we can do it, even if the author of the schema above does not declare any "children" element.

Look at this schema file named "children.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.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>

The following XML file (named "Myfamily.xml"), used data from two different schema ingredients, "family.xsd" and "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.w3schools.com children.xsd">

<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
<childname>Cecilie</childname>
</children>
</person>

<person>
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>

</persons>

Above the XML file is valid, which is due to schema "family.xsd" We allowed after "lastname" elements optional elements to expand "person" element.

<Any> and <anyAttribute> can be used to create scalable document! They have the ability to make the document did not contain a statement of the main XML schema through additional elements.