Jump to content

simple elements vs attributes


Guest Ragnarok

Recommended Posts

Guest Ragnarok

Hi,I've a question regarding attributes and simple elements in XSD. For what I know by now everything that can be expressed as an attribute could be expressed as a child simple element of the complex element whose characteristics we want to write. Beside of that simple elements are more flexible if someone wants to change them later, as they can more easily be converted into complex elements, or can have their type redefined. In fact I don't see why do attributes exist, the XML standard would be less complex to process, and every parser implemented would be therefore more simple. Nevertheless if they are in the standard probably there is a good reason for that, and they should be used in some circumstances.So, my question is, when should each type of data be used?Thank you.

Link to comment
Share on other sites

Different XML standards (XHTML coming into mind) often provide a default behaviour when a certain element is not supported by an implementaion for whatever reason. This behaviour is usually to expose the contents of unsupported elements, process the childs nodes as if the element never existed, and ignore unsupported attributes.So, if we have

<video src="video.avi"><object data="video.avi">Your browser can't play this video. Please upgrade, or verify that you have a player avaiable.</object></video>

If the browser doesn't understand the <video/> element, it will attempt to use the <object/> element instead. If that too fails, it will issue the text message.If the "src" attribute was insetad an element, like:

<video><src>video.avi</src><object data="video.avi">Your browser can't play this video. Please upgrade, or verify that you have a player avaiable.</object></video>

Then an implementaion that doesn't support the <video/> element (and thus likely won't support the <src/> element), the browser will attempt to use the <object/> element, but users which use the <object/> element are also going to see "video.avi" on the page.Attributes are also more compact i.e. they take less bytes from your computer's memory.

<a href="/">t</a>

is shorter than

<a><href>/</href>t</a>

so in cases where you're absolutely sure you won't expand something, an attribute is a good thing to use. URIs, as in the case of the "href" attribute are such a case. The URI standard is already estabilished for use with this attribute, and it's very unlikely to change soon. IRIs are a possible replacements, but they too have been made in a way that would allow them to easily be embedded in attributes, so the possibility of URIs evolving to IRIs is not a reason this attribute would go.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...