XML Attributes
What is XML Attribute ?
- XML Attributes are just like “HTML Attributes“.
- “XML Attributes” provides additional information about “XML Element“.
- Attributes Consists of name/value pairs associated with an element.
- 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>
Explanation :
- Consider above example, In this Example “Nickname” is Attribute Of Element “name“.
- “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>