Can Element be same as Attribute name?

G

gogaz

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
 
M

Mukul Gandhi

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
 
M

Martin Honnen

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.
 
P

Peter Flynn

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top