An XML Schema describes the structure of an XML document.
The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.
An XML Schema:
- defines elements that can appear in a document
- defines attributes that can appear in a document
- defines which elements are child elements
- defines the order of child elements
- defines the number of child elements
- defines whether an element is empty or can include text
- defines data types for elements and attributes
- defines default and fixed values for elements and attributes
dog.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.consultants.com/it"
xmlns:tns="http://www.consultants.com/it" elementFormDefault="qualified">
<element name="cdogs" type="tns:Dogs"/>
<element name="dogs" type="tns:Dogs"/>
<complexType name="Dogs">
<sequence>
<element name="dog" type="tns:Dog" maxOccurs="5" minOccurs="0"/>
</sequence>
</complexType>
<complexType name="Dog">
<sequence>
<element name="name" type="string" maxOccurs="1" minOccurs="1"/>
<element name="age" type="int" maxOccurs="1" minOccurs="1"/>
</sequence>
<attribute name="color" type="string" use="required"/>
<attribute name="mobile" type="string" use="required"/>
</complexType>
</schema>
dog.xml
<?xml version="1.0" encoding="UTF-8"?>
<dogs xmlns="http://www.consultants.com/it"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.consultants.com/it dogs.xsd">
<dog color="red" mobile="939393939">
<name>name</name>
<age>11</age>
</dog>
<dog color="pink" mobile="93939393">
<name>name</name>
<age>22</age>
</dog>
<dog color="blue" mobile="939393939">
<name>name</name>
<age>33</age>
</dog>
<dog color="pink" mobile="939393939">
<name>name</name>
<age>44</age>
</dog>
<dog color="yellow" mobile="939393939">
<name>name</name>
<age>55</age>
</dog>
</dogs>
No comments:
Post a Comment