Switch to full style
XML Schema tutorial
Post a reply

lesson9: XSD Complex Types Indicators

Fri Mar 06, 2009 8:08 pm

XSD Complex Types Indicators :

There are seven indicators in XSD. There are three indicator groups.
1. Order indicators :
Define the element's order inside your complex type.

Any indicator
Elements can appear in any order. And all must appears.
Code:
<xs:element name="student">
  <xs:complexType>
    <xs:all>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="age" type="xs:integer"/>
    </xs:all>
  </xs:complexType>
</xs:element>

This XSD code defines an element called "student" that has a complex type consisting of a "xs:all" element. The "xs:all" element contains a list of elements that can occur in any order, in this case, the "name" element, and the "age" element. The "name" and "age" elements are defined as having the data type "xs:string" and "xs:integer" respectively. This means that the "name" element must contain a string value and the "age" element must contain an integer value. The "xs:all" element is used to specify that the elements within it can occur in any order. This means that the "name" element and the "age" element can appear in any order within the "student" element. The "xs:all" element is useful when the order of elements is not essential for the XML document.




Choice Indicator
Only one element of elements inside <choice> can appear.
Code:
<xs:element name="human">
  <xs:complexType>
    <xs:choice>
      <xs:element name="man" type="manType"/>
      <xs:element name="women" type="womenType"/>
    </xs:choice>
  </xs:complexType>
</xs:element>


This XSD code defines an element called "human" that has a complex type consisting of a "xs:choice" element. The "xs:choice" element allows you to specify the number of possible elements within the "human" element, but only one of them can occur at a time. In this example, the "xs:choice" element contains two possible elements: "man" and "women". Each of these elements has a type defined by "manType" and "womenType" respectively. This means that the "human" element must contain either a "man" element of type "manType" or a "women" element of type "womenType". The "xs:choice" element is useful when you want to allow for a number of different options within a complex type, but only one of them can occur at a time. It's worth noting that the "xs:choice" element is mutually exclusive, which means that if one option is chosen, the others are not allowed. Note that the "xs:choice" element is useful when you want to specify that an element can have one of a number of possible elements as children, but only one of them can occur at a time.


Sequence Indicator
Elements should appear in the same order it was defined.
Code:
<xs:element name="student">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="age" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>


2. Occurrence Indicators
Define the number of times an element occurs. maxOccurs & minOccurs attributes are used to define occurrence .Both equal 1 by default.

maxOccurs Indicator:
Define the Maximum number of element occurs.
Code:
<xs:element name="department">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Professor" type="xs:string" maxOccurs="unbounded"/>
      <xs:element name="Subject" type="xs:string" maxOccurs="50"
  />
    </xs:sequence>
  </xs:complexType>
</xs:element>

This XSD code defines an element called "department" that has a complex type consisting of a "xs:sequence" element. The "xs:sequence" element contains a list of elements that must appear in the order they are defined, in this case, the "Professor" element, followed by the "Subject" element. The "Professor" and "Subject" elements are defined as having the data type "xs:string". This means that both elements must contain a string value. The "Professor" element has the attribute "maxOccurs" set to "unbounded" which means that it can occur any number of times within the "department" element. While, the "Subject" element has the attribute "maxOccurs" set to "50" which means that it can occur a maximum of 50 times within the "department" element. The "maxOccurs" attribute allows you to specify the maximum number of times an element can occur within its parent element.



Note: you can set your limits to unlimited. You use maxOccurs="unbounded".

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<department>
<Professor>Jozef Philp</ Professor>
<Professor>Amr Ali</ Professor>
<Professor>Tom Philp</ Professor>
<Subject>Course1</Subject>
<Subject> Course2</Subject>
<Subject> Course3</Subject>
<Subject> Course4</Subject>
</department>

As you can above. The department has many professors and many subjects. Same cases to minOccurs attribute.

minOccurs Indicator:
Define the minimum number of element occurs.

3.Group Indicators
Allow you to define a set of related elements and attributes.

Elements groups:
For example look for the following XSD.
Code:
<xs:group name="studentgroup">
  <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="age" type="xs:integer"/>
    <xs:element name="gpa" type="xs:string"/>
  </xs:sequence>
</xs:group>



You can define your element group and reference it from other definitions.

Code:
<xs:group name="studentgroup">
  <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="age" type="xs:integer"/>
    <xs:element name="gpa" type="xs:string"/>
  </xs:sequence>
</xs:group>

<xs:element name="student" type="studentmoreType"/>
<xs:complexType name="studentmoreType">
  <xs:sequence>
    <xs:group ref="studentgroup"/>
    <xs:element name="Address" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

This code defines a group called "studentgroup" which contains a sequence of three elements: "name", "age", and "gpa". The "name" and "Address" elements are of type "xs:string", and the "age" element is of type "xs:integer". Additionally, this XSD code defines an element called "student" that has a complex type called "studentmoreType". The "studentmoreType" complex type contains a sequence of elements, where the first element is a reference to the previously defined "studentgroup" group, and the second element is called "Address" and is of type "xs:string". This means that the "student" element must contain a sequence of elements that includes the elements defined in the "studentgroup" group, and also an "Address" element of type "xs:string". This structure allows for the reuse of the group, which makes the schema more maintainable and readable. This code is useful for example, if you want to create an XML document that describes students and their personal information, where students have a name, age and gpa, and also an address. The group "studentgroup" could be used in other parts of the schema, for example, for creating an element for the student's parents, which also have a name, age, and address.

Note that the "xs:group" element is used to group elements together and can be referenced multiple times in the schema using the "xs:group ref" attribute. This allows you to define a group of elements once and reuse it multiple times, which makes the schema more maintainable and readable. Also, the "xs:sequence" element in both group and complexType is used to specify that the elements within it must appear in the order they are defined. If you want the elements to be optional or repeatable, you can use the "minOccurs" and "maxOccurs" attributes of the "xs:element" element, respectively.

In this code snippet, the "name" and "gpa" elements are of type xs:string, meaning they can contain any string value. If you want to enforce more constraints on these elements, you can use a more specific data type or a pattern facet to restrict the allowed values.
Using the "xs:complexType" and "xs:sequence" elements in this code allows for creating more complex and nested XML structures, which can be useful for describing more detailed and sophisticated XML documents.


Attribute Groups:
Code:
<xs:attributeGroup name=" studentgroup">
  <xs:attribute name="name" type="xs:string"/>
  <xs:attribute name="age" type="xs:integer"/>
  <xs:attribute name="gpa" type="xs:string"/>
</xs:attributeGroup>


This XSD code defines an attribute group called "studentgroup" which contains three attributes: "name", "age", and "gpa". The "name", "age" and "gpa" attributes are of type "xs:string" and "xs:integer" respectively. An attribute group is a way to group attributes and reuse them multiple times in the schema using the "xs:attributeGroup ref" attribute. It allows you to define a set of attributes once and reuse them multiple times, which makes the schema more maintainable and readable. In this case, the "name", "age" and "gpa" attributes can be used on any element in the schema that references this attribute group using the "ref" attribute. The use of the attribute group is especially useful when you have a set of attributes that are used repeatedly in different parts of the schema.


You can define your attribute group and reference it from other definitions.

Code:
<xs:attributeGroup name="studentgroup">
  <xs:attribute name="name" type="xs:string"/>
  <xs:attribute name="age" type="xs:integer"/>
  <xs:attribute name="gpa" type="xs:string"/>
</xs:attributeGroup>

<xs:element name="student">
  <xs:complexType>
    <xs:attributeGroup ref="studentgroup"/>
  </xs:complexType>
</xs:element>


This XSD code defines an attribute group called "studentgroup" which contains three attributes: "name", "age", and "gpa". The "name", "age" and "gpa" attributes are of type "xs:string" and "xs:integer" respectively. It also defines an element called "student" that has a complex type. The complex type contains a reference to the previously defined attribute group "studentgroup" using the "xs:attributeGroup ref" attribute. This means that the "student" element must contain the attributes defined in the "studentgroup" attribute group, which are "name", "age" and "gpa". This structure allows for the reuse of the attribute group, which makes the schema more maintainable and readable. It allows you to define a set of attributes once and reuse them multiple times, especially when you have a set of attributes used repeatedly in different schema parts.



Post a reply
  Related Posts  to : lesson9: XSD Complex Types Indicators
 Complex Numbers     -  
 Complex numbers calculator (C++)     -  
 lesson5: XSD Complex elements     -  
 lesson7: XSD Complex Text-Only Elements     -  
 solve the complex numbers and do operations on it     -  
 lesson6: XSD Complex Empty Elements     -  
 lesson8: XSD Complex Type Mixed Content     -  
 Types of Registers     -  
 Types of Pointers in C++     -  
 assembler directives types     -