XML Attributes



What is XML Attribute ?

  1. XML Attributes are just like “HTML Attributes“.
  2. XML Attributes” provides additional information about “XML Element“.
  3. Attributes Consists of name/value pairs associated with an element.
  4. Attributes are attached to the start-tag, but not to the end-tag

XML Attributes : Simple Example

<name nickname="Mickey">
  <first>John</first>
  <middle>Mike</middle>
  <last>Smith</last>
</name>

XML Attributes : Additional Information About Element

Explanation :

  1. Consider above example, In this Example “Nickname” is Attribute Of Element “name“.
  2. Nickname” Attribute have value associated with it - “Mickey“.
name      ===> Root Element
nickname  ===> Attribute-Name
Mickey    ===> Attribute-Value
first     ===> Sub Element Of "name"
middle    ===> Sub Element Of "name"
last      ===> Sub Element Of "name"

Illegal Ways of Using XML Attributes

Way 1 : Specifying Only Name Of Attribute

<input checked>

Way 2 : Value Associated With Name must be quoted

<input checked=true>

Way 3 : Mismatched Single and Double Quotes

<inputchecked="true'>

Way 4 : Cannot have Same Attribute Associated With Same Element

<personname="Pritesh"name="taral">

Way 5 : Don’t use Double Quotes inside Attribute Value.

<tipname='Mike "Beautiful" Flower'>

we can instead use -

<tipname='Mike "Beautiful" Flower'>

XML Elements vs. Attributes

Using Attribute -

<name nickname="Mickey">
  <first>John</first>
  <middle>Mike</middle>
  <last>Smith</last>
</name>

Using Element -

<name>
  <nickname>Mickey</nickname>
  <first>John</first>
  <middle>Mike</middle>
  <last>Smith</last>
</name>