#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    Midrand, South Africa
    Posts
    154
    Rep Power
    14

    Question Attributes vs Elements


    After reading the "Idiot's Guide to XML", I was still confused about certain aspects of implementing XML.

    Which is better to use?

    <person name="John" surname="Doe"></person>

    or

    <person>
    <name>John</name>
    <surname>Doe</surname>
    </person>
    Neil Craig
    Software Developer
    SourceCode Techonolgy Holdings Inc
    South Africa
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    I read the same book. Pretty bland
    It's completely up to you, though. It may depend on how you want to use the data, which way makes more sense, or if you just want to use fewer characters. You could also user /> instead of the closing tag...
    I'd say how you're planning to use the data should be your guide.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    3
    Rep Power
    0
    I think you should use elements for most things (especially properties of the parent element, like "name" for a "person"). IT seems to me that attributes are only useful if you want to make a distinction between instances of the same element, for example <phoneno type="home"> and <phoneno type="work">.
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Sure, but if you're wanting to model a real-life object, such as a person, you (a person) have an attributes, one of which is your name. Your name isn't some sub-part of who you are, not the way a driver's license number or address would be. It may be a unique way to identify yourself, but unless you're up to no good you've only got one =).
    That's another criteria for attribute vs. element. You can have more than one element of the same type, but only one attribute. So middlename would probably be an element, since some people out there have several.

IMN logo majestic logo threadwatch logo seochat tools logo