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