Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Basic XML question [Thread Next] Re: Basic XML questionTo: NULL Date: 7/11/2009 1:05:00 AM > What are these different ways of defining nodes called? And when is it > good to use the one instead of the other? An element which does not have content (whether or not it has attributes) may be expressed either with an open and close tag pair: <foo></foo> or with an "empty element" tag <foo/> These two forms are completely identical semantically; the difference is almost purely one of convenience and personal preference. Why "almost"? Well, it is officially preferred (ie, there is a SHOULD keyword in the XML Recommendation) that the empty element tag be used only for elements whose DTD or Schema specification says they can never contain content (ie, that in a Valid document they will always be empty). But I don't believe anyone has ever actually followed that preference, since the empty-element tag is a trifle more efficient to type by hand or process by machine. So that proposed convention seems to have died due to lack of support from the community, and in practice the two forms really are completely equivalent. I'm presuming that's what you meant by "open" versus "closed". Note that it's completely orthogonal to the decision of whether the element should carry data as attributes, as content, or both. That's a decision made when the language which XML is being used to implement is designed, based on a combination of stylistic preferences and practical concerns. Attributes have their values normalized to some degree, have no meaningful order, and are limited to name-value pairs, whereas contained nodes may include any desired mix of text/elements/comments/processing instructions, do not normalize the text, and do reliably report their order as a possibly significant piece of information. Both approaches have advantages and disadvantages. Generally, by convention, attributes are used for information that modifies how the element they're attached to should be interpreted, while the information that is logically contained by that element is placed in its content ("child nodes")... but there are exceptions, which is where the stylistic element comes in. Do what makes sense given what the document is intended to mean, but if there's any doubt you should probably lean toward child nodes -- they give you more options for future enhancement. If you need more guidance on this, websearching for "XML children versus attributes" or similar phrases will find many other discussions of this trade-off. Hope that helps. | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
