Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML Schema any element


May 28, 2021 XML Schema


Table of contents


XSD

This section introduces you to the use of XML Schema any elements that can be used to extend XML documentation.


The element gives us the ability to expand the XML documentation through elements that are not specified by schema!


The element

The element gives us the ability to expand the XML documentation through elements that are not specified by schema!

The following example is a reference to a fragment from XML schema named "family.xsd". I t shows a declaration for the "person" element. By using the element, we can extend the content of the "person" by any element (after the element:

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

Tip: If you add a complex type to the list of complex types, you can extend the original complex type by adding a "0" or "0" to any minOccurs.

Now we want to use the "children" element to extend the "person" element. In this case, we can do so, even if the author of the schema above does not declare any "children" elements.

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.w3cschools.com"
xmlns="http://www.w3cschools.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 ingredients 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.w3cschools.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>

This XML file above is valid because schema "family.xsd" allows us to extend the "person" element by selecting elements after the "lastname" element.

Both slt;any and anyAttribute are available to produce scalable documents! They give the document the ability to contain additional elements that have not been declared in the main XML schema.

In the next section, we'll cover the anyAttribute element.