XML Schema Tuto...login
XML Schema Tutorial
author:php.cn  update time:2022-04-20 14:13:02

XML Schema any element


XSD <any> Elements


##><any> elements give us the ability to extend with elements that are not specified by the schema XML document! The

h2><any> element
<any> element gives us the ability to extend the XML document with elements that are not specified by the schema!

The following example is a fragment quoted from the XML schema named "family.xsd". It shows a declaration for the "person" element. By using the <any> element, we can extend the content of "person" by any element (after <lastname>):

<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 extend the "person" element with the "children" element. In this case we can do this even if the author of the above schema did not declare any "children" elements.

Please 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") uses components from two different schemas, "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>

The above XML file is valid due to the schema " family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element.

<any> and <anyAttribute> can both be used to make scalable documents! They give the document the ability to contain additional elements not declared in the main XML schema.