Attributes vs. Values

Discussion in 'XML' started by jason.cipriani@gmail.com, Mar 1, 2008.

  1. Guest

    I'm just recently starting to use XML for storing data, and I am
    wondering when it is more appropriate to use child node values instead
    of node attributes -- and in general, what are good ways to structure
    the XML. For example, let's say I want to store information about the
    furniture in a room. This room has a red, wooden chair. It has a
    green ceramic lamp. It also has a white end table made of, I don't
    know, let's say human bone (scary!). So one way I could say this in
    XML is:

    <furniture>
    <chair>
    <color>red</color>
    <material>wood</material>
    </chair>
    <lamp>
    <color>green</color>
    <material>ceramic</color>
    </lamp>
    <table>
    <type>endtable</type>
    <color>white</color>
    <material>bone</material>
    </table>
    </furniture>

    Ok... but I could also do, say:

    <item what="chair">
    ...
    </item>
    <item what="lamp">
    ...
    </item>
    <item what="table">
    ...
    </item>

    Or for that table, I can remove "type" child node and make that an
    attribute, maybe that is more appropriate:

    <item what="table" subtype="endtable">...</item>

    Or I could just use attributes for everything (this one seems to be
    the most natural to me...):

    <furniture>
    <chair color="red" material="wood"/>
    <lamp color="green" material="ceramic"/>
    <table color="white" material="bone" type="end"/>
    </furniture>

    Or I could combine the above two:

    <furniture>
    <item what="chair" color="red" material="wood"/>
    <item what="lamp" color="green" material="ceramic"/>
    <item what="table" color="white" material="bone" type="end"/>
    </furniture>

    I could even do this:

    <item what="chair">
    <color value="red"/>
    <material value="ceramic"/>
    </item>

    Or this:

    <item>
    <property name="what" value="chair"/>
    <property name="color" value="red"/>
    <property name="material" value="ceramic"/>
    </item>

    Or this:

    <item>
    <property name="what">chair</property>
    <property name="color">red</property>
    <property name="material">ceramic</property>
    </item>

    Or any number of the infinite other possibilities I can think of...

    When does it make sense to use values vs. attributes? When does it
    make sense to have different child node types (like "chair", "lamp",
    "table") rather than the same child node types but different
    attributes (all child nodes are "item" but they have a "what"
    attribute)?

    It's really confusing because I have so many options.

    Thanks,
    Jason
    , Mar 1, 2008
    #1
    1. Advertising

  2. Peter Flynn Guest

    wrote:
    > I'm just recently starting to use XML for storing data, and I am
    > wondering when it is more appropriate to use child node values instead
    > of node attributes


    There is an article on this in the XML FAQ:
    http://xml.silmaril.ie/developers/attributes/

    ///Peter
    Peter Flynn, Mar 1, 2008
    #2
    1. Advertising

  3. Guest

    On Mar 1, 4:56 pm, Peter Flynn <> wrote:
    > wrote:
    > > I'm just recently starting to use XML for storing data, and I am
    > > wondering when it is more appropriate to use child node values instead
    > > of node attributes

    >
    > There is an article on this in the XML FAQ:http://xml.silmaril.ie/developers/attributes/


    Thanks for that! That's exactly the kind of info I was looking for.

    "Beginners always ask this question. Those with a little experience
    express their opinions passionately. Experts tell you there is no
    right answer."

    Makes me feel a little more secure about asking this question; and
    reading through everything else makes me feel a lot better about just
    picking whatever is easier in the situation.

    Jason
    , Mar 1, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    1
    Views:
    468
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,787
    bruce barker
    Jan 31, 2007
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    358
    Bruno Desthuilliers
    Jun 20, 2007
  4. Kyle Schmitt
    Replies:
    3
    Views:
    177
    Kyle Schmitt
    Jul 24, 2007
  5. Jayden
    Replies:
    16
    Views:
    468
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page