Switch to full style
XML Schema tutorial
Post a reply

lesson5: XSD Complex elements

Thu Mar 05, 2009 11:36 pm

XSD Complex elements :

XSD Complex element contains other elements and attributes .


How to define complex elements:

For example the following XML file:
Code:
<student>
<name>Jozef samir</name>
<age>32</age>
</student>

<xs:element name="student">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="age" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>


This is XML defines a "student" element which contains two child elements: "name" and "age". The "name" element is of type "xs:string", which means it can hold a string value, and the "age" element is of type "xs:long", which means it can hold a long integer value. It also defines the structure of the elements, with xs:element and xs:complexType, which defines a complex type element, which can contain other elements and attributes. It also specifies the structure of the elements and attributes, using xs:sequence, which says that the elements should appear in a specific sequence. The name "Jozef Samir" and the age "32" are the values of the elements.



You define it by another way:
Code:
<xs:element name="student" type="studentType" >
  <xs:complexType name="studentType" >
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="age" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>


The difference between the two ways, in the first method, you can only create one element of this complex type, but in the second way, you can define many elements using the same type.
For example :
Code:
<xs:element name="student" type="studentType" >
<xs:element name=" professor  " type="studentType" >


  <xs:complexType name="studentType" >
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="age" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>



Note: <xs:sequence> Tag means element should appear in the same order in an XML document.

2. XSD Complex Type extension:
You can extend the complex type and add more elements to it.
Code:
<xs:element name="student" type="studentType" >
  <xs:complexType name="studentType" >
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="age" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>

<xs:complexType name="moreinfo">
  <xs:complexContent>
    <xs:extension base="studentType">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>


This is also an example of XML code, which defines a "student" element of type "studentType". The "studentType" is defined as a complexType, which contains two child elements, "name" and "age", both of type "xs:string" and "xs:long" respectively. This code also defines an additional complexType named "moreinfo" which is an extension of studentType. It uses xs:extension, which allows a complexType to inherit the elements and attributes of another complexType. The "moreinfo" complexType contains additional elements "address", "city" and "country" of type "xs:string". The xs:sequence element indicates that these elements should appear in a specific order. This code defines a more complex structure for the student element, which includes not only the basic information of "name" and "age" but also more information like "address", "city", "and country".



Post a reply
  Related Posts  to : lesson5: XSD Complex elements
 lesson7: XSD Complex Text-Only Elements     -  
 lesson6: XSD Complex Empty Elements     -  
 Complex Numbers     -  
 Complex numbers calculator (C++)     -  
 lesson9: XSD Complex Types Indicators     -  
 solve the complex numbers and do operations on it     -  
 lesson8: XSD Complex Type Mixed Content     -  
 Add elements to the end of an array     -  
 clone arrayList elements     -  
 elements of a GridBagLayout organized     -  

Topic Tags

XSD Elements