Complex Elements
Complex elements are those which contain other elements as children or these elements have attributes. Empty elements and elements containing only text are also considered complex. The following is an examples of complex element
<employee grade="senior"> <id>745821</id> <empName>Tim</empName> <salary>35000</salary> <domain>Insurance</domain> </employee>
Define Complex Type in XML schema
Complex types can be defined in XML schema as demonstrated in this example:
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="id" type="xs:string"/> <xs:element name="empName" type="xs:string"/> <xs:element name="salary" type="xs:string"/> <xs:element name="domain" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
Define empty elements
Consider the following XML element:
<employee empId="157117" />
This empty element can be defined in XML schema as follows:
<xs:element name="employee"> <xs:complexType> <xs:attribute name="empId" type="xs:positiveInteger"/> </xs:complexType> </xs:element>
Here we can see that in the definition we dont define any child elements. We just define the attribute.
Define text only elements
Consider the following XML element:
<domain>BFS</domain>
The above element can be defined in XML schema as follows:
<xs:element name="domain"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string" /> </xs:simpleContent> </xs:complexType> </xs:element>
Here, we define that the content of the complex type is simple. In schema terms simple means plain text. The xs:extension is used to declare the type of the data.
Define Mixed Content
Sometimes an XML element can contain elements and text combined. Consider the following example:
<employeeInfo> The employee <id>134567</id> belongs to <domain>BFS</domain> and his salary is <salary>56430</salary> </employeeInfo>
The above fragment can be defined in XML schema as follows:
<xs:element name="employeeInfo"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="id" type="xs:positiveInteger"/> <xs:element name="domain" type="xs:string"/> <xs:element name="salary" type="xs:positiveInteger"/> </xs:sequence> </xs:complexType> </xs:element>
In the defined XML schema we use the attribute mixed and set its value as true. This indicates that the XML element can contain other elements and simple text as it children
XSD Indicators
Using XSD Indicator a desicion can be made about how to use Complex types elements in a XML document. The indicators are basically divided into three groups :
- Order indicators
- Occurrence indicators
- Group indicators
Let us know look at each of these categories.
Order Indicators
The order indicators define the ordering of the elements or the sequence in which the elements appear in the XML document. This group of indicators has three types of indicators :
- All
- Choice
- Sequence
All
Using the All indicator allows the child elements to appear in any order. However, it sets a restriction that each child must appear only once. Lets look at an example :
<xs:element name="employee"> <xs:complexType> <xs:all> <xs:element name="empId" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="salary" type="xs:integer"/> <xs:element name="domain" type="xs:string"/> </xs:all> </xs:complexType> </xs:element>
Choice
The Choice indicators specifies that out of all the child elements one can appear. Lets look at an example:
<xs:element name="employee"> <xs:complexType> <xs:choice> <xs:element name="manager" type="manager"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> </xs:element>
Sequence
This indicator specfies that the child elements should follow the specified order :
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="empId" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="salary" type="xs:integer"/> <xs:element name="domain" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
Occurrence Indicators
The occurrence indiactor defines the number of times an element can appear. This group has two differetn types:
- maxOccurs
- minOccurs
maxOccurs
The maxOccurs element sets an upper limit on the number of times an element can upper. Lets looks at an example:
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="empId" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="salary" type="xs:integer"/> <xs:element name="domain" type="xs:string"/> <xs:element name="project" type="xs:string" maxOccurs="10"/> </xs:sequence> </xs:complexType> </xs:element>
minOccurs
The minOccurs element sets the lower imit on the number of times an element can upper. Lets looks at an example:
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="empId" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="salary" type="xs:integer"/> <xs:element name="domain" type="xs:string"/> <xs:element name="project" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>
Group Indicators
Group indicators allow us to define a group of elements
- Group name
- attributeGroup name
Group Name
The group indicator is used to define a group of elements. Once defined the group can be referenced in other places within the XML schema. Lets look at an example :
<xs:group name="projectGroup"> <xs:sequence> <xs:element name="projectname" type="xs:string"/> <xs:element name="technology" type="xs:string"/> </xs:sequence> </xs:group>
In the above example we define a group named projectGroup. Now we will see how to refer this group in other places :
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="empId" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="salary" type="xs:integer"/> <xs:element name="domain" type="xs:string"/> <xs:group ref="projectGroup"/> </xs:sequence> </xs:complexType> </xs:element>
After making a reference to the projectGroup the employee element gets a total of six child elements:
empId, name, salary, domain, projectname, technology.
Attribute Groups
The attribute group is used to group together a set of attributes. Once defined the group can be referenced in other elements to define the attributes for a element. Lets look at an example :
<xs:attributeGroup name="empAttrGroup"> <xs:attribute name="designation" type="xs:string"/> </xs:attributeGroup> <xs:complexType name="employee"> <xs:attributeGroup ref="empAttrGroup"/> </xs:complexType>
The Element
With the use of the any element an XML schema can can extend another XML schema. Lets look at an example :
Filename : employee.xsd
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="empId" type="xs:positiveinteger"/> <xs:element name="name" type="xs:string"/> <xs:element name="salary" type="xs:positiveinteger"/> <xs:element name="domain" type="xs:string"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>
Filename : children.xsd
<xs:element name="project"> <xs:complexType> <xs:sequence> <xs:element name="projectName" type="xs:string" /> <xs:element name="technologyUsed" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element>
The XML file can use components from both the files:
<employee> <empId>143233</empId> <name>John</name> <salary>34500</salary> <domain>Insurance</domain> <project> <projectName>ISD</projectName> <technologyUsed>.NET</technologyUsed> </project> </employee>
XSD Data Types
String
The string data type is used to store characters and strings. For e.g:
<table border="1"> <tr> <td>XML Schema</td> <td><xs:element name="name" type="xs:string"/></td> </tr> <tr> <td>XML</td> <td><name>John</name></td> </tr> </table>
Date
The date type represents a date. The date represented by using the date datatype is specified in the following format : YYYY-MM-DD.
For e.g:
<table border="1"> <tr> <td>XML Schema</td> <td><xs:element name="joiningDate" type="xs:date"/> </td> </tr> <tr> <td>XML</td> <td><joiningDate>2007-03-27</joiningDate></td> </tr> </table>
Numeric
The numeric data types are of two types:
- Decimal
The decimal data type is used to represent decimal numbers. For e.g:
<table border="1"> <tr> <td>XML Schema</td> <td><xs:element name="price" type="xs:decimal"/></td> </tr> <tr> <td>XML</td> <td><price>678.50</price></td> </tr> </table>
The integer data type is used to represent integers. For e.g:
<table border="1"> <tr> <td>XML Schema</td> <td><xs:element name="salary" type="xs:integer"/></td> </tr> <tr> <td>XML</td> <td><salary>678.50</salary></td> </tr> </table>
Other Datatypes
- Boolean
The boolean datatype represents the value true or false.E.g:
XML Schema | |
XML | John |
also read: