Main BLOGGER
Google
WWW THIS BLOG
Monday, November 07, 2005
 
XML Schema Design [ Best Practice ]

Quoted from http://www.xfront.com/

 

  1. 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.

 

  1. 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:

<xsd:element name="Publication" abstract="true" 
             type="PublicationType"/>
 
<xsd:element name="Book" substitutionGroup="Publication" 
             type="BookType"/>
<xsd:element name="Magazine" substitutionGroup="Publication" 
             type="MagazineType"/>

Next, declare a container type for each element, and have the container type holding the head element be the root of the type hierarchy:

<xsd:complexType name="PublicationContainer">
    <xsd:sequence>
        <xsd:element ref="Publication" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>
 
<xsd:complexType name="BookContainer">
    <xsd:complexContent>
        <xsd:restriction base="PublicationContainer">
            <xsd:sequence>
                <xsd:element ref="Book" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:restriction>
    </xsd:complexContent>
</xsd:complexType>
 
<xsd:complexType name="MagazineContainer">
    <xsd:complexContent>
        <xsd:restriction base="PublicationContainer">
            <xsd:sequence>
                <xsd:element ref="Magazine" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:restriction>
    </xsd:complexContent>
</xsd:complexType>

Lastly, declare <Catalogue> to be of type PublicationContainer:

<xsd:element name="Catalogue" type="PublicationContainer"/>

Here's a sample instance document:

<Catalogue>
    <Book>...</Book>
    <Magazine>...</Magazine>
    <Book>...</Book>
</Catalogue>

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:

<Catalogue xsi:type="BookContainer">
    <Book>...</Book>
    <Book>...</Book>
    <Book>...</Book>
</Catalogue>

 

 

 




<< Home

Powered by Blogger

Google
WWW THIS BLOG