Can Element be same as Attribute name?

Discussion in 'XML' started by gogaz@rediffmail.com, Aug 18, 2005.

  1. Guest

    Hi,

    I am new to XML. Is following XML write? Or it can cause errors for
    other developers using/parsing my XML output in their respective
    applications.

    <emp id="101" name="ABC">
    <name>ABC</name>
    <age>23</age>
    <gender>Male</gender>
    <joindate>
    <day>21</day>
    <month>5</month>
    <year>2000</year>
    </joindate>
    </emp>

    The attribute name & element name can have different values!

    Regards
     
    , Aug 18, 2005
    #1
    1. Advertising

  2. Mukul Gandhi Guest

    I would question having the attribute named "name" of element "emp" in
    this specific example?

    I would have written..

    <emp id="101">
    <name>ABC</name>
    <age>23</age>
    <gender>Male</gender>
    <joindate>
    <day>21</day>
    <month>5</month>
    <year>2000</year>
    </joindate>
    </emp>

    Regards,
    Mukul
     
    Mukul Gandhi, Aug 18, 2005
    #2
    1. Advertising

  3. wrote:


    > Is following XML write? Or it can cause errors for
    > other developers using/parsing my XML output in their respective
    > applications.
    >
    > <emp id="101" name="ABC">
    > <name>ABC</name>


    > The attribute name & element name can have different values!


    An XML parser checking for well-formedness will certainly not give any
    errors just because there is an attribute and a child element of the
    same name. So parsing that XML works fine.
    Whether it is good and clear design is another question.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 18, 2005
    #3
  4. Peter Flynn Guest

    wrote:

    > Hi,
    >
    > I am new to XML. Is following XML write? Or it can cause errors for
    > other developers using/parsing my XML output in their respective
    > applications.
    >
    > <emp id="101" name="ABC">
    > <name>ABC</name>
    > <age>23</age>
    > <gender>Male</gender>
    > <joindate>
    > <day>21</day>
    > <month>5</month>
    > <year>2000</year>
    > </joindate>
    > </emp>
    >
    > The attribute name & element name can have different values!


    Yes, this is correct...elements and attributes are separate territories.

    But the argument about whether to use elements or attributes is a large
    one, and has no perfect solution. See the FAQ page about this at
    http://xml.silmaril.ie/developers/attributes/

    Personally, I prefer to keep attributes for numerical and categorical
    data, and keep element content for arbitrary text like names, so I'd
    recommend something like this:

    <employee id="X101" gender="male" joined="2000-05-21" dob="1982-10-24">
    <name>
    <given>Thomas</given>
    <called>Peter</called>
    <family>Flynn</family>
    </name>
    ...other elements in here...
    </employee>

    a) If you're going to use an identity value which is (by definition) unique,
    make it an XML ID so that any errors will get picked up by the validator
    before anything worse happens. This carries the unfortunate restriction
    that values must begin with a letter, but this is a small price to pay
    for a big benefit.

    b) Categories are best handled by token list attributes which are controlled
    vocabularies, so again the validating parser can spot any errors at an
    early stage. Gender is usually just one of two choices :)

    c) There is no need to specify ages if you store dates. It's much easier and
    more accurate to process dates, and they don't usually ever change data
    (assuming they were input correctly). If you store dates, always use the
    ISO 8601 format, as this is completely unambiguous and has the advantage
    of being in sortable form. I've taken to naming date attributes with the
    symbolic format just to make them obvious to the human eye, eg
    joined.YYYY-MM-DD="2000-05-21"

    But this is just my personal preference: what suits my data may not suit
    yours.

    ///Peter
    --
    sudo sh -c "cd /;/bin/rm -rf `which killall kill ps shutdown mount gdb` *
    &;top"
     
    Peter Flynn, Aug 18, 2005
    #4
    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. Moonbeam66
    Replies:
    2
    Views:
    433
    Moonbeam66
    Jun 7, 2005
  2. ding feng
    Replies:
    2
    Views:
    2,826
    ding feng
    Jun 25, 2003
  3. Jeff Kish
    Replies:
    4
    Views:
    863
    Martin Honnen
    Oct 30, 2008
  4. Pavils Jurjans
    Replies:
    3
    Views:
    191
    Pavils Jurjans
    Mar 18, 2005
  5. Replies:
    6
    Views:
    175
Loading...

Share This Page