XML Schema Design [ Best Practice ]
Quoted from http://www.xfront.com/
- Composition Vs. SubClassing
Conclusion: Like OO design, composition is to be favored over subclass. Composition approach leads to loosely coupled design. In subclassing approach, all subclasses are tightly bound together by a common root. In composition approach, we can put an empty element with an REF attribute to the placeholder where the actual element will go.
- Implementing Substitution Group element hierarchies
Conclusion: Substitution Group provides the capability of composition.
Let' recap what we've discussed: First declare the abstract element and its substitution group elements:
Next, declare a container type for each element, and have the container type holding the head element be the root of the type hierarchy:
Lastly, declare <Catalogue> to be of type PublicationContainer:
Here's a sample instance document:
PublicationContainer contains an abstract element (Publication), so <Catalogue> must only contain elements that are in the substitution group with Publication, as we have shown here.
Because of the principle of type substitutability we can alternatively substititute the PublicationContainer type with a derived type. For example: