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
"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
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
> 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