3

XML schema defined as follow, and suppose i have to get sal where dept="lab" and id="001".

<start>
    <emp>
        <dept>lab</dept>                    
        <id>001</id>                        
        <sex>male</sex>                 
        <sal>5k</sal>                         
    </emp>                                          
</start>

another way,

<start>                                     
    <emp dept="lab">                                           
        <id sr="001">                        
            <sex>male</sex>                 
            <sal>5k</sal>
        </id>                       
    </emp>                                          
</start>

I think the second way is far better to parse but it is advisable to avoid attribute, why? What do you say?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user1010399
  • 2,258
  • 5
  • 30
  • 42
  • there isn't any standard rule but i just googled on web and found that most people suggested to use element. – user1010399 Oct 28 '11 at 09:19
  • This is a possible duplicate of: http://stackoverflow.com/questions/33746/xml-attribute-vs-xml-element –  May 16 '13 at 01:30
  • Possible duplicate of [XML attribute vs XML element](https://stackoverflow.com/questions/33746/xml-attribute-vs-xml-element) – omegastripes Feb 28 '18 at 05:33

2 Answers2

1

There are 3 main reasons to avoid attributes:

  1. Attributes cannot contain multiple values but child elements can have multiple values.
  2. Attributes cannot contain tree structure but child element can.
  3. Attributes are not easily expandable. If you want to change in attribute's vales in future, it may be complicated.
  4. Attributes cannot describe structure but child elements can.
  5. Attributes are more difficult to be manipulated by program code.
  6. Attributes values are not easy to test against a DTD, which is used to define the legal elements of an XML document.

This is why it is advised to go forward for elements not attributes...

0

Some people don't like attributes. They say so volubly, and you have found their remarks on the Web.

If you disagree, feel free to ignore those people and use attributes all you want. (If it matters, you can always infer that the designers of SGML and XML agreed with you, and not with those who dislike attributes.)

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65