xml attributes - what is XML attribute?

what is xml attribute

attributes are name/value pairs associated with an element.the name and the value are each strings,and no element can contain two attributes with the same name.

simply attributes provide additional information about an element.

xml elements can have more then one attributes.

                   <information date="22/1/2013"  >
                   
                               <name> sandeep </name>
                               
                               <age> 25 </age>
                               
                   </information>

how use xml attribute - xml attributes rules

the attributes value must be in quotes.you can use either single or double quotes.you can use single on some attributes and double on others, but you can't mix them in a single attribute.

<male gender="male">

or equal to :

<male gender='male'>
<male gender='male">          wrong statement(mismatching delimiters).

attribute names must be unique per element. and you can use character entities with attribute value.

<doctor name="sandeep &quot;kumar&quot; nehra">


XML elements versus attributes

the xml element or an attribute both are used to represent data .there are no better choice about when to use elements or when to use attributes.

xml element attribute example code

                <information  date="12/7/2013" >
                   
                        <name> sandeep </name>
                        
                        <age> 25 </age>
                        
                        <company>oracle </company>
                        
               </information>          
             
             
                <information>
                        
                        <date>12/7/2013</date>
                       
                        <name> sandeep </name>
                        
                        <age> 25 </age>
                        
                        <company>oracle </company>
                        
               </information>          
             
             

both example provide the same information.the first example provide information through date attribute and the last example provide information through as an element.

some of the problems with using attributes are :--

  • attributes cannot contain tree structures.
  • attributes are unordered. elements are ordered.
  • elements are more extensible in the face of future changes(attribute are not).