Hi Folks,
Consider this XML document:
<Items>
<Aquarium>
<Tank>40 gallon capacity</Tank>
</Aquarium>
<Abrams>
<Tank>M-1A2C</Tank>
</Abrams>
</Items>
Notice that there are two <Tank> elements. They have completely different meanings: the first is a tank that we put fish in, the second is a tank that is used in military battles.
Same tag name, totally different meanings.
Are you okay with that? That is, are you okay with using the same tag name for two different things?
Below I make this argument: Never use the same tag name for two different things.
Here's why:
Recall the DRY (Don't Repeat Yourself) principle. Consider an XML Schema for the above XML. Assume the type for the aquarium tank is different than the type for the military tank:
<xs:element name="Title" type="AquariumTankType"/>
<xs:element name="Title" type="MilitaryTankType"/>
The DRY principle says: Declare once, ref often.
But that's more "something to consider as part of the design" not really a "principle" that must be followed in all cases.
That is, in the XML Schema globally declare the Title element once and ref to it multiple times. But we can't declare Title only once since they have different types. There must be at least one Title element that is locally declared. The DRY principle must be violated. That is bad design.
No, it is simply a feature of XSD schema syntax.
If you use DTD you would have a single global declaration (which would have to be of the union of both uses)
If you use Relax NG you can globally declare tank-fish and tank-army patterns that both use the same element name but each used in their respective contexts.
Question: Beside violating the DRY principle, are there other reasons to avoid using the same tag name for different things?
Well it's clearly something to avoid just on the grounds of trying not to be confusing, but it's not exactly uncommon, look at all the web pages that use <div> for essentially everything,
David
Here's a better design:
<Items>
<Aquarium>
<Tank>40 gallon capacity</Tank>
</Aquarium>
<Abrams>
<BattleTank>M-1A2C</BattleTank>
</Abrams>
</Items>
Now an XML Schema can declare Tank and BattleTank globally and ref to them. DRY is intact.
as I say the Relax NG form of this would use essentially the same structure whether or not the element names were the same.
Question: do you agree that XML documents should not use the same tag name for different things?
/Roger
_______________________________________________________________________
XML-DEV is a publicly archived, unmoderated list hosted by OASIS
to support XML implementation and development. To minimize
spam in the archives, you must subscribe before posting.
[Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
Or unsubscribe: xml-dev-unsubscribe@lists.xml.org
subscribe: xml-dev-subscribe@lists.xml.org
List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php