Jump to content

Confused about Elements and Attributes. Help !


akuash

Recommended Posts

Hello EveryoneI have recently started learning XML and very much confused about the use of elements and attributes. I have read many articles and some rules of thumb but still unable to get a clear picture of when to use what. For example one good rule of thumb is to use single valued data as attributes and multiple valued data as elements. Another to represent data as elements and meta data as attributes. Some articles say describe several other approaches or rules. I have several questions in mind and will much appreciate if any one can help me in understanding1) What if the data is single valued ? Should I make it an attribute or an element. E.g. Marital Status of a person is single valued at the same time its data. What will be a difference in an attribute and element in such a case ? 2) For example Date of birth is also a single valued data but many forums suggested making separate elements for day month and year and some suggested making a date of birth element and day year and month its attributes. What is the difference How will either approach help ? 3) what is more useful for search operations and how ? If I want to design it in such a way that its more useful and efficient for searching what shall I do. For example what difference an element or an attribute of date of birth will make for a search operation I.e. if I want to search people born in 1980 or blah blah. I will much appreciate your help.Cheers

Link to comment
Share on other sites

If you need to print the data,frequently check the data or update the data,if the data is tangible it is best to use it as element. otherwise is use it as attribute.

Link to comment
Share on other sites

An element can only have one copy of every attribute. e.g.

<element attribute1="first occurrence of attribute1 on this (first) element" attribute2="first occurrence of attribute2 on this (first) element" /><element attribute1="first occurrence of attribute1 on this (second) element" />

is allowed, but

<element attribute1="first occurrence of attribute1 on this (first) element" attribute1="second occurrence of attribute1 on this (first) element"/><element attribute1="first occurrence of attribute1 on this (second) element" />

is not allowed.This is an XML restriction. Beyond that restriction, you don't really have any.The rest is up to you, and for the users of your language to follow. All "rules of thumb" are intended to make it easier for people to understand your language and/or make it easier for them to write programs using it, but if you have clear enough documentation, you don't need to follow any of it.1. A person's current martial status is a single value, but if you wanted to also present their martial status history, it might be better to use an element instead, due to the restriction described above.2. If you're going to use a single value, you should require a standard formatting for it, such as xsd:date. This will make it easier for XML Schema supporting parsers (such as C#'s built in ones) to parse your XML. For parsers that don't support XML Schema, you could split the date into a single element with attributes or an element with child elements, but either way, you're making it easier for schema-less parsers at the expense of schema-aware parsers.3. Depends on what is more efficient with the parser you're using. There is no generic rule for this. This is one of the primary reasons XML is not really suitable for a database.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...