Back to the 2023 paper

Module I: Introduction to Web Technologies and Architectures

202314m

Explain simple and complex data types in XML with suitable example?

Worked SolutionAI Assisted

Data types in XML Schema (XSD)

XML Schema defines the structure and data types of an XML document. Types are either simple or complex.

1. Simple types

A simple type holds only text/data. It has no child elements and no attributes.

Built-in simple types: xs:string, xs:integer, xs:decimal, xs:boolean, xs:date, xs:time, xs:float.

<xs:element name="name"  type="xs:string"/>
<xs:element name="age"   type="xs:integer"/>
<xs:element name="dob"   type="xs:date"/>

Matching XML: <name>Ravi</name> <age>21</age> <dob>2004-05-12</dob>

User-defined simple type (restriction):

<xs:simpleType name="marksType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="100"/>
  </xs:restriction>
</xs:simpleType>
<xs:element name="marks" type="marksType"/>

Simple types can also be created as a list or union of other types.

2. Complex types

A complex type may contain child elements, attributes, or both, so it describes structured data.

<xs:element name="student">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name"  type="xs:string"/>
      <xs:element name="age"   type="xs:integer"/>
      <xs:element name="marks" type="xs:integer"/>
    </xs:sequence>
    <xs:attribute name="roll" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

Matching XML:

<student roll="CS101">
  <name>Ravi</name>
  <age>21</age>
  <marks>88</marks>
</student>

Content models: xs:sequence (fixed order), xs:choice (one of several), xs:all (any order). Complex types can also mix text and elements (mixed="true") or be empty with only attributes.

Comparison

Feature Simple type Complex type
Child elements No Yes
Attributes No Yes
Content Text value only Elements, attributes, or both
Example <age>21</age> <student roll="1">...</student>
Reuse Named simpleType Named complexType

Note that an attribute itself always has a simple type.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.