Attributes vs. Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jason.cipriani@gmail.com

    Attributes vs. Values

    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>woo d</material>
    </chair>
    <lamp>
    <color>green</color>
    <material>ceram ic</color>
    </lamp>
    <table>
    <type>endtabl e</type>
    <color>white</color>
    <material>bon e</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="endtab le">...</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="ceram ic"/>
    <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="ceram ic"/>
    <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">cha ir</property>
    <property name="color">re d</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
  • Peter Flynn

    #2
    Re: Attributes vs. Values

    jason.cipriani@ gmail.com 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:


    ///Peter

    Comment

    Working...