Open In App

What is the difference between a simple element and a complex element in XML ?

Improve
Improve
Like Article
Like
Save
Share
Report

The eXtensible Markup Language (XML) is a data storage toolbox, a configurable vehicle for any type of information, and a dynamic and open standard that is used by everyone from bankers to webmasters. To be self-descriptive, XML was created to store and convey data.

Markup is data that is added to a document to improve its meaning in certain ways, such as identifying the pieces and how they interact. XML elements can have attributes. These attributes can be used to add information about the element.

In this article, we are going to read about the different types of elements in XML. You can write an XML, for eg. about a book in 2 ways such as:

Method 1:

<country capital="New Delhi" states="28" 
    climate="tropical monsoon"> 
</country>

Method 2:

<country>
    <capital> New Delhi </capital>
      <states> 28 </states>
      <climate> tropical monsoon </climate>
</country> 

Both of the examples above contain the same information, but it is best to avoid using attributes in XML and instead use sub-elements.

On the basis of the attributes and sub-elements, XML elements can be divided into 2 types :

Simple Element: An XML element with no attributes or sub (child) elements is known as a simple element. A simple element can be used to declare a simple element. Simple types aren’t allowed to have element content or carry attributes. They can only have content that is directly enclosed between the opening and closing tags of the element. They aren’t allowed to have traits or children.

There are constraints on the allowable values for elements and attributes in simple type declarations, these are called facets. When an element or attribute expects a specific type of input, the constraints might be utilized. For instance, telephone input only allows numbers with a maximum of ten digits, a person’s names can only be in alphabets, with the first word capitalized and the rest in small characters.

For eg, consider XML of a FruitInventory

XML




<FruitInventory>
    <Fruit tid="1">
        <ItemNumber>1000</ItemNumber>
        <FruitName>
            <CommonName>Mango</CommonName>
            <ScientificName>
                Mangifera indica
            </ScientificName>
        </FruitName>
        <Description>
            A mango is an edible stone fruit
            produced by the tropical tree
            Mangifera indica which is believed
            to have originated from
            northeastern India.
        </Description>
        <Price>₹ 30/kg.</Price>
        <Quantity>50</Quantity>
        <Type>drupe</Type>
    </Fruit>
</FruitInventory>


The simple elements defined inside the FruitInventory are:

XML




<xsd:element name="Description" type="xs:string"/>
<xsd:element name="Price" type="xs:decimal"/>
<xsd:element name="Quantity" type="xs:integer"/>


Complex Elements: An XML element with at least one attribute of at least one child element is called a Complex Element. A Complex Element can be used to declare a simple element. Complex kinds can include characteristics, contain additional components, and mix elements and text, among other things.

Child components or attributes are defined in a complex form of data. An element can have its own data types and can be declared as its own data datatype. The content of a complex element can be empty elements, other elements, text, or a combination of elements and text.

A complicated type is a logical unit that combines a group of simple types. A customer type could include information such as the customer number, name, street address, town, city, and zip code. A complex type can also reference other complex types or element and attribute groups.

Note, for eg: let us again consider the FruitInventory

XML




<FruitInventory>
    <Fruit tid="1">
        <ItemNumber>1000</ItemNumber>
        <FruitName>
            <CommonName>Mango</CommonName>
            <ScientificName>Mangifera indica</ScientificName>
        </FruitName>
 
        <Description>
            A mango is an edible stone fruit produced
            by the tropical tree Mangifera indica
            which is believed to have originated
            from northeastern India.
        </Description>
        <Price>₹ 30/kg.</Price>
        <Quantity>50</Quantity>
        <Type>drupe</Type>
    </Fruit>
</FruitInventory>


The complex elements defined inside the FruitInventory are:

XML




<xsd:element name="FruitName">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="CommonName" type="xsd:string"/>
            <xsd:element name="ScientificName" type="xds:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>


Difference Between Simple and Complex Elements:

Simple Element

Complex Element

It does not have any attributes. Should have at least one attribute if no sub child is present.
It does not have any sub-child. Should have at least one sub child if no attribute is present.
A simple element can be declared with a simple datatype. A simple element can be declared with a complex datatype.
They contain self-closing tags. Do not contain self-closing tags.
It can be used to define an ID, which is an integer with a maximum value limit. can be used to define a student and fill in their details like DOB, name, etc.


Last Updated : 25 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads