Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Wed, 16 Apr 2008 08:54:42 +0200,    group: microsoft.public.xml        back       


Attribute or Element?   
Hi,

I'm working with creating a XML Schema. I almost directly became uncertain 
wheter/when to use attributes and when to use elements. Which is best:

this:

<person>
    <name>adam</name>
    <other stuff>
    ...
</person>

or this:

<person name="adam">
    <other stuff>
    ...
</person>

Are there any recommendations on when to use what?

regards

Carl
date: Wed, 16 Apr 2008 08:54:42 +0200   author:   Carl

Re: Attribute or Element?   
"Carl"  wrote in message 
news:1D4B1579-DBB4-46DB-AB64-EC95376FCC3B@microsoft.com...
> Hi,
>
> I'm working with creating a XML Schema. I almost directly became uncertain 
> wheter/when to use attributes and when to use elements. Which is best:
>
> this:
>
> <person>
>    <name>adam</name>
>    <other stuff>
>    ...
> </person>
>
> or this:
>
> <person name="adam">
>    <other stuff>
>    ...
> </person>
>
> Are there any recommendations on when to use what?
>
> regards
>
> Carl
>
There are lots of differing views on  this subject. In general I, and many 
others, prefer attributes for properties that are "one offs" and meta data 
and elements when there could be many instances. For example in your example 
I would have name as an attribute but if you were describing a book you 
would want multiple authors and therefore show them as elements, probably 
grouped under an authors element. The exception to this is when the property 
can contain large amounts of text, then using attributes can be unwieldy 
especially as you cannot have the equivalent of a CDATA section with an 
attribute's value so need to escape some characters.

It also helps for later retrieval and processing if you make things as 
granular as possible such as splitting name into family name and given name.
In the case of something like phone numbers where you could choose to have 
attributes for homePhone, workPhone and mobilePhone for instance I would 
tend to go with:
<phoneNumbers>
  <phoneNumber type="home" value="0123456789"/>
  <phoneNumber type="work" value="1234567890"/>
  <phoneNumber type="mobile" value="2345678901"/>
</phoneNumbers>
although many would prefer
<phoneNumbers>
  <phoneNumber type="home" 0123456789</phoneNumber>
  <phoneNumber type="work" >1234567890</phoneNumber>
  <phoneNumber type="mobile">2345678901</phoneNumber>
</phoneNumbers>

both leave room to easily add other types of phone number.

-- 

Joe Fawcett (MVP - XML)
http://joe.fawcett.name
date: Wed, 16 Apr 2008 06:46:32 -0700   author:   Joe Fawcett am

Re: Attribute or Element?   
Carl wrote:
> Hi,
> 
> I'm working with creating a XML Schema. I almost directly became 
> uncertain wheter/when to use attributes and when to use elements. Which 
> is best:

This is a FAQ. See http://xml.silmaril.ie/developers/attributes/

///Peter
date: Wed, 16 Apr 2008 17:08:34 +0100   author:   Peter Flynn

Re: Attribute or Element?   
> There are lots of differing views on  this subject. In general I, and many 
> others, prefer attributes for properties that are "one offs" and meta data 
> and elements when there could be many instances. For example in your 
> example I would have name as an attribute but if you were describing a 
> book you would want multiple authors and therefore show them as elements, 
> probably grouped under an authors element. The exception to this is when 
> the property can contain large amounts of text, then using attributes can 
> be unwieldy especially as you cannot have the equivalent of a CDATA 
> section with an attribute's value so need to escape some characters.
>
> It also helps for later retrieval and processing if you make things as 
> granular as possible such as splitting name into family name and given 
> name.
> In the case of something like phone numbers where you could choose to have 
> attributes for homePhone, workPhone and mobilePhone for instance I would 
> tend to go with:
> <phoneNumbers>
>  <phoneNumber type="home" value="0123456789"/>
>  <phoneNumber type="work" value="1234567890"/>
>  <phoneNumber type="mobile" value="2345678901"/>
> </phoneNumbers>
> although many would prefer
> <phoneNumbers>
>  <phoneNumber type="home" 0123456789</phoneNumber>
>  <phoneNumber type="work" >1234567890</phoneNumber>
>  <phoneNumber type="mobile">2345678901</phoneNumber>
> </phoneNumbers>
>
> both leave room to easily add other types of phone number.
>
> -- 

Thanks for your help, It's helpful to see the reasoning regarding this.

regards,

Carl
date: Thu, 17 Apr 2008 09:45:51 +0200   author:   Carl

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us