- From: Rick Jelliffe <rjelliffe@a...>
- To: xml-dev <xml-dev@l...>
- Date: Thu, 20 Jan 2022 10:51:34 +1100
The example shows the folly of the idea that you can always happily "just model the data" without considering the likely application needs or gotchas.
If you are using DTDs, you can only use a different name for everything with a different content model.
If your code is not using XPath or some API with convenient access to the context, then shared names may have a programming cost.
So univocal names are usually better, to reduce potential gotchas. Namespaces can help this.
But what about XSD, where elements with the same name can have different types depending on the type of their parent? Such elements are, to an extent, semantically different in an unnamed way (otherwise they would have the same types.) Should elements derived differrently from the same base type always have a different element names too?
Cheers Rick
On Thu, 20 Jan. 2022, 05:53 Roger L Costello, < costello@m...> wrote: 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. 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.
Question: Beside violating the DRY principle, are there other reasons to avoid using the same tag name for different things?
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.
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@l...
subscribe: xml-dev-subscribe@l...
List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php
[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]
|