Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Can Element be same as Attribute name?

Reply
Thread Tools

Can Element be same as Attribute name?

 
 
gogaz@rediffmail.com
Guest
Posts: n/a
 
      08-18-2005
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

 
Reply With Quote
 
 
 
 
Mukul Gandhi
Guest
Posts: n/a
 
      08-18-2005
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-18-2005


http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
Peter Flynn
Guest
Posts: n/a
 
      08-18-2005
(E-Mail Removed) 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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't code a css class that makes a span element the same size as a button element Cal Who ASP .Net 5 04-26-2010 05:35 PM
Attribute 'onchange' is not a valid attribute of element 'TextBox' Joe Kovac ASP .Net 2 05-02-2007 01:11 PM
Validation (XHTML 1.0 Transitional): Attribute 'height' is not a valid attribute of element 'table' perspolis ASP .Net 1 01-23-2007 07:57 AM
Attribute 'ms_2d_layout' is not a valid attribute of element 'tabl =?Utf-8?B?TWFyaw==?= ASP .Net 4 12-14-2006 06:39 AM
Validation (XHTML 1.0 Transitional): Attribute 'leftmargin' is not a valid attribute of element 'body'. anonymous ASP .Net 1 08-02-2006 08:05 AM



Advertisments