Thread: XML attributes

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    XML attributes


    Hello,

    I'm new to XML and I've been reading about elements vs. attributes.

    In a case like:

    <size>
    <width unit="cm">9</width>
    </size>

    Would it be possible to express the same information, without using any attribute keys. Would it be correct:

    <size>
    <width>9 cm</width>
    </size>

    or:

    <size>
    <width>
    <unit>cm</unit>
    <value>9</value>
    </width>
    </size>

    Thank you very much in advance!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    27
    Rep Power
    0
    well usage of attributes or elements is usually decided by the data you are trying to model.

    certain entity is PART of the data, then it is advisable to make it an element.

    Hope this helps.
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,298
    Rep Power
    9400
    The first and third ones have the same data but expressed differently so they're both fine - as priyankagound said it comes down to the model you have to follow.

    The second one isn't good because now somebody has to parse the "9 cm" string to get the length and units. They might have to deal with "9cm" or "9 CM" or some other format that can arise from making something a text field.

    In general with XML you want to break down the data into as many individual components as you can: keep the "9" separate from the "cm". Whether you use attributes or elements isn't as important.

IMN logo majestic logo threadwatch logo seochat tools logo