Diffrence Between Element and Attributes in XML

This topic differentiate between Element and Attributes in XML.
  • 2111

Element Vs Attributes

Element is part of the DTD. It is declared within a DocType  tag with the tag name of Element . But  Attribute are always declared inside the element tag . That is why is also know as child tag . So there is  no rule about when  declared child element and when declared Attributes.

For Example :

<person sex = "male ">

<firstname>pulkit</firstname>

<lastname>thakur</lastname>

</person>

 

Note:- In  the above declared example "sex" work like a Attributes.

 

Another Example:

 

<person>

<sex> female </sex>

<firstname>pulkit</firstname>

<lastname>thakur</lastname>

</person>

 

Note: In the second example "sex" declared as child element . So there is no rule about this that when we declared Attributes and when child tag.

 

My Favorite Way

 

One most important think and best way to use it as a child tag in XML. And  as a Attributes in a HTML

There is some Example.

 

Example1 


<note date="12/11/2002">
  <to>pulkit</to>
  <from>vijay</from>
  <heading>Reminder me </heading>
  <body>Don't forget me this year</body>
</note>

 

Note: Here above declared "date" is work like a Attributes.

 

Example2


note>
  <date>12/11/2002</date>
  <to>pulkit</to>
  <from>vijay</from>
  <heading>Reminder me</heading>
  <body>Don't forget me this year</body>
</note>

 

Note: In the exmple2 date declared as element.

 

Example 3:


<note>
  <date>
    <day>15</day>
    <month>01</month>
    <year>2007</year>
  </date>
  <to>pulkit</to>
  <from>vijay</from>
  <heading>Reminder me </heading>
  <body>Don't forget me this year</body>
</note>

 

Note: This is one the best way to use the element tag "date"


Avoid using attributes?

Why Should you avoid using attributes?

These are the problems when we use  attributes are:

  • attributes contain only one value not a  (child elements )
  • it is  not easily expandable (for  any future modification)
  • attributes has no their structure  but  (child elements have)
  • managing attributes in a program is difficult
  • attribute values are  difficult to test against a DTD

If we use Attributes in a element  Attributes  has no end tag . That is why it difficult  to read out it.

For Example:

<note day="15" month="01" year="2007"
to="pulkit" from=" vijay" heading="Reminder me "
body="Don't forget me this year>
</note>

Note : In the above declared example several Attributes declared within a not Element . so mostly user confuse in this.

An Exception in  Attribute Rule

Some Exception always occur in any language. so  in xml Attribute have also some exception these are .

When we  assign ID references to elements. These ID references can be used to access XML elements in similar way the NAME or ID attributes in HTML. This example 

<messages>
<note id="s201">
  <to>pulkit</to>
  <from>vijay</from>
  <heading>Reminder me </heading>
  <body>Don't forget me this year</body>
</note>

<note id="s202">
  <to>pulkitq</to>
  <from>vijay</from>
  <heading>Re: Reminder me </heading>
  <body>I am  other person</body>
</note>
</messages>

The ID in  examples is  a counter, or a unique identifier, to identify the different- different  notes in the XML file, and not a part of the note data.

further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

 

© 2020 DotNetHeaven. All rights reserved.