OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: XML Schemas: Best Practices

[ Lists Home | Date Index | Thread Index ]
  • From: "Roger L. Costello" <costello@mitre.org>
  • To: xml-dev@lists.xml.org
  • Date: Fri, 20 Oct 2000 12:14:06 -0400

[All: one thing which will be helpful is that if you use terminology
from other domains, e.g., UML, Java, OO, then please define your terms
so that we can all have a common understanding of the terminology being
used.]

Excellent message Curt!  Contrasting points of view is the key to making
the Best Practices document robust.  Thanks!

Let me see if I can summarize your points Curt.  Let's take your
Person/Book example.  Here's a sample Person component:

    <Person id="rbach>
        <Name>Richard Bach</Name>
    </Person>

The Person element contains an id attribute to uniquely identify it.

You mentioned that in UML this would be represented as a "Person class"
containing a "Name attribute". I don't know UML, but I imagine this to
correspond to a box labeled Person, and contained within the box a field
called Name.

Now that we have a Person component we can reuse it in creating the Book
component:

    <Book>
        <Title>Illusions</Title>
        <Author idref="rbach"/>
    </Book>

This might be read as: "Book is comprised of a Title followed by an
Author.  The Author is a Person whose name is Richard Bach."

In this design there are two components (classes), Person and Book, and
the Author element bridges (associates) the two components.  I do see
how this is very OO-like, and how an OO (UML) model might map directly
to this design.

I believe that your concern is with how the above Person/Book instance
should be defined in a schema.  Let's see how each of the three design
approaches would model the Person/Book.

Russian Doll Design

This design says to keep things compact and simple by inlining element
declarations.  Thus, here is how the Person/Book might be represented
using this design:

   <element name="Person">
      <complexType>
         <sequence>
            <element name="Name" type="string"/>
         </sequence>
         <attribute name="id" type="ID" use="required"/>
      </complexType>
   </element>

   <element name="Book">
      <complexType>
         <sequence>
            <element name="Title" type="string"/>
            <element name="Author">
               <complexType>
                  <attribute name="idref" type="IDREF" use="required"/>
               </complexType>
            </element>
            <attribute name="id" type="ID" use="required"/>
         </sequence>
      </complexType>
   </element>

As is characteristic of the Russian Doll design, all the elements and
attributes are nested together in a tidy, compact bundle.

Salami Slice Design:

With this design all the components are "spread out", declared
individually, and then aggregated back together.  Here's how the
Person/Book might be represented using this design:

   <element name="Name" type="string"/>

   <attribute name="id" type="ID"/>

   <element name="Person">
      <complexType>
         <sequence>
            <element ref="cat:Name"/>
         </sequence>
         <attribute ref="cat:id" use="required"/>
      </complexType>
   </element>

   <element name="Title" type="string"/>

   <attribute name="idref" type="IDREF"/>

   <element name="Author">
      <complexType>
         <attribute ref="cat:idref" use="required"/>
      </complexType>
   </element>

   <element name="Book">
      <complexType>
         <sequence>
            <element ref="cat:Title"/>
            <element ref="cat:Author"/>
         </sequence>
      </complexType>
   </element>
 
The notable characteristic of this design is that all the elements are
global.  Consequently, their namespaces will always be exposed in
instance documents, irrespective of the value of the elementFormDefault
"switch".

Venetian Blind Design

With this approach we strive for the reuse that the Salami Slice design
provides, and the ability to toggle exposure/hiding that the Russian
Doll design provides.  This is accomplished by "spreading out"
components as types, and nesting element declarations
within the types.  Here's how Person/Book might be represented using
this design:

   <complexType name="Person">
      <sequence>
         <element name="Name" type="string"/>
      </sequence>
      <attribute name="id" type="ID" use="required"/>
   </complexType>

   <complexType name="Author">
      <attribute name="idref" type="IDREF" use="required"/>
   </complexType>

   <complexType name="Book">
      <sequence>
         <element name="Title" type="string"/>
         <element name="Author" type="cat:Author"/>
      </sequence>
   </complexType>

Note that the elements are nested within the types.  This enables us to
toggle on/off namespace exposure in instance documents.

As an exercise, I wrote schemas and instance documents for each of the
designs.  And for each design I wrote one version which exposes
namespaces in instance documents and one which hides the namespaces. 
Thus, I created six pairs of schema/instance-document.  

What I found most dramatic was the difference in the instance documents
where the schema was designed to hide (localize) the namespaces. The
instance documents corresponding to the Russian Doll and Venetian Blind
designs are simple and unencumbered by namespace qualifiers:

<?xml version="1.0"?>
<cat:Catalog xmlns:cat="http://www.catalog.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd">
   <Person id="rbach">
      <Name>Richard Bach</Name>
   </Person>
   <Book>
      <Title>Illusions</Title>
      <Author idref="rbach"/>
   </Book>
</cat:Catalog>

The namespaces are hidden within the schema.  On the other hand, the
instance document for the Salami Slice design was filled with many
namespace qualifiers:

<?xml version="1.0"?>
<cat:Catalog xmlns:cat="http://www.catalog.org"
         xmlns:a="http://www.person-book.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd
                     http://www.person-book.org
                     PersonBook.xsd">
   <a:Person a:id="rbach">
      <a:Name>Richard Bach</a:Name>
   </a:Person>
   <a:Book>
      <a:Title>Illusions</a:Title>
      <a:Author a:idref="rbach"/>
   </a:Book>
</cat:Catalog>

This is, of course, due to the fact that this design declares all the
elements globally.  This is a really good example to demonstrate the
inability of the Salami Slice design to hide namespaces.

I believe that this demonstrates that the designs are quite general, and
that the Venetian Blind design does seem to provide the greatest "bang
for the buck".
     
Below, for those interested, are the six pairs of schema/instance
documents.  

Thanks again Curt!  Please let me know if this hasn't addressed your
issues.  /Roger

******************************************************
Russian Doll Design/Expose Namespaces/Schema
******************************************************

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
        targetNamespace="http://www.catalog.org"
        elementFormDefault="qualified"
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
        xsi:schemaLocation=
                        "http://www.w3.org/2000/10/XMLSchema
                         http://www.w3.org/2000/10/XMLSchema.xsd"
        xmlns:cat="http://www.catalog.org">
   <element name="Catalog">
      <complexType>
         <sequence>
            <element name="Person">
               <complexType>
                  <sequence>
                     <element name="Name" type="string"/>
                  </sequence>
                  <attribute name="id" type="ID" use="required"/>
               </complexType>
            </element>

            <element name="Book">
               <complexType>
                  <sequence>
                     <element name="Title" type="string"/>
                     <element name="Author">
                        <complexType>
                           <attribute name="idref" type="IDREF" 
                                      use="required"/>
                        </complexType>
                     </element>
                     <attribute name="id" type="ID" use="required"/>
                  </sequence>
               </complexType>
            </element>
         </sequence>
      </complexType>
   </element>
</schema>

******************************************************
Russian Doll Design/Expose Namespaces/Instance Document
******************************************************

<?xml version="1.0"?>
<Catalog xmlns="http://www.catalog.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd">
   <Person id="rbach">
      <Name>Richard Bach</Name>
   </Person>
   <Book>
      <Title>Illusions</Title>
      <Author idref="rbach"/>
   </Book>
</Catalog>

******************************************************
Russian Doll Design/Hide Namespaces/Schema
******************************************************

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
        targetNamespace="http://www.catalog.org"
        elementFormDefault="unqualified"
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
        xsi:schemaLocation=
                        "http://www.w3.org/2000/10/XMLSchema
                         http://www.w3.org/2000/10/XMLSchema.xsd"
        xmlns:cat="http://www.catalog.org">
   <element name="Catalog">
      <complexType>
         <sequence>
            <element name="Person">
               <complexType>
                  <sequence>
                     <element name="Name" type="string"/>
                  </sequence>
                  <attribute name="id" type="ID" use="required"/>
               </complexType>
            </element>

            <element name="Book">
               <complexType>
                  <sequence>
                     <element name="Title" type="string"/>
                     <element name="Author">
                        <complexType>
                           <attribute name="idref" type="IDREF" 
                                      use="required"/>
                        </complexType>
                     </element>
                     <attribute name="id" type="ID" use="required"/>
                  </sequence>
               </complexType>
            </element>
         </sequence>
      </complexType>
   </element>
</schema>

******************************************************
Russian Doll Design/Hide Namespaces/Instance Document
******************************************************

<?xml version="1.0"?>
<cat:Catalog xmlns:cat="http://www.catalog.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd">
   <Person id="rbach">
      <Name>Richard Bach</Name>
   </Person>
   <Book>
      <Title>Illusions</Title>
      <Author idref="rbach"/>
   </Book>
</cat:Catalog>

******************************************************
Salami Slice Design/Expose Namespaces/Schema
******************************************************

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
        targetNamespace="http://www.catalog.org"
        elementFormDefault="qualified"
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
        xsi:schemaLocation=
                        "http://www.w3.org/2000/10/XMLSchema
                         http://www.w3.org/2000/10/XMLSchema.xsd"
        xmlns:cat="http://www.catalog.org">

   <element name="Name" type="string"/>

   <attribute name="id" type="ID"/>

   <element name="Person">
      <complexType>
         <sequence>
            <element ref="cat:Name"/>
         </sequence>
         <attribute ref="cat:id" use="required"/>
      </complexType>
   </element>

   <element name="Title" type="string"/>

   <attribute name="idref" type="IDREF"/>

   <element name="Author">
      <complexType>
         <attribute ref="cat:idref" use="required"/>
      </complexType>
   </element>

   <element name="Book">
      <complexType>
         <sequence>
            <element ref="cat:Title"/>
            <element ref="cat:Author"/>
         </sequence>
      </complexType>
   </element>
   <element name="Catalog">
      <complexType>
         <sequence>
            <element ref="cat:Person"/>
            <element ref="cat:Book"/>
         </sequence>
      </complexType>
   </element>
</schema>

******************************************************
Salami Slice Design/Expose Namespaces/Instance Document
******************************************************

<?xml version="1.0"?>
<Catalog xmlns="http://www.catalog.org"
         xmlns:cat="http://www.catalog.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd">
   <Person cat:id="rbach">
      <Name>Richard Bach</Name>
   </Person>
   <Book>
      <Title>Illusions</Title>
      <Author cat:idref="rbach"/>
   </Book>
</Catalog>

******************************************************
Salami Slice Design/Hide Namespaces/Schema
******************************************************

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
        targetNamespace="http://www.catalog.org"
        elementFormDefault="unqualified"
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
        xsi:schemaLocation=
                        "http://www.w3.org/2000/10/XMLSchema
                         http://www.w3.org/2000/10/XMLSchema.xsd"
        xmlns:cat="http://www.catalog.org">

   <element name="Name" type="string"/>

   <attribute name="id" type="ID"/>

   <element name="Person">
      <complexType>
         <sequence>
            <element ref="cat:Name"/>
         </sequence>
         <attribute ref="cat:id" use="required"/>
      </complexType>
   </element>

   <element name="Title" type="string"/>

   <attribute name="idref" type="IDREF"/>

   <element name="Author">
      <complexType>
         <attribute ref="cat:idref" use="required"/>
      </complexType>
   </element>

   <element name="Book">
      <complexType>
         <sequence>
            <element ref="cat:Title"/>
            <element ref="cat:Author"/>
         </sequence>
      </complexType>
   </element>
   <element name="Catalog">
      <complexType>
         <sequence>
            <element ref="cat:Person"/>
            <element ref="cat:Book"/>
         </sequence>
      </complexType>
   </element>
</schema>

******************************************************
Salami Slice Design/Hide Namespaces/Instance Document
******************************************************

<?xml version="1.0"?>
<cat:Catalog xmlns:cat="http://www.catalog.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd">
   <cat:Person cat:id="rbach">
      <cat:Name>Richard Bach</cat:Name>
   </cat:Person>
   <cat:Book>
      <cat:Title>Illusions</cat:Title>
      <cat:Author cat:idref="rbach"/>
   </cat:Book>
</cat:Catalog>

******************************************************
Venetian Blind Design/Expose Namespaces/Schema
******************************************************

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
        targetNamespace="http://www.catalog.org"
        elementFormDefault="qualified"
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
        xsi:schemaLocation=
                        "http://www.w3.org/2000/10/XMLSchema
                         http://www.w3.org/2000/10/XMLSchema.xsd"
        xmlns:cat="http://www.catalog.org">

   <complexType name="Person">
      <sequence>
         <element name="Name" type="string"/>
      </sequence>
      <attribute name="id" type="ID" use="required"/>
   </complexType>

   <complexType name="Author">
      <attribute name="idref" type="IDREF" use="required"/>
   </complexType>

   <complexType name="Book">
      <sequence>
         <element name="Title" type="string"/>
         <element name="Author" type="cat:Author"/>
      </sequence>
   </complexType>

   <complexType name="Catalog">
      <sequence>
         <element name="Person" type="cat:Person"/>
         <element name="Book" type="cat:Book"/>
      </sequence>
   </complexType>
   
   <element name="Catalog" type="cat:Catalog"/>
</schema>

******************************************************
Venetian Blind Design/Expose Namespaces/Instance Document
******************************************************

<?xml version="1.0"?>
<Catalog xmlns="http://www.catalog.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd">
   <Person id="rbach">
      <Name>Richard Bach</Name>
   </Person>
   <Book>
      <Title>Illusions</Title>
      <Author idref="rbach"/>
   </Book>
</Catalog>

******************************************************
Venetian Blind Design/Hide Namespaces/Schema
******************************************************

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
        targetNamespace="http://www.catalog.org"
        elementFormDefault="unqualified"
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
        xsi:schemaLocation=
                        "http://www.w3.org/2000/10/XMLSchema
                         http://www.w3.org/2000/10/XMLSchema.xsd"
        xmlns:cat="http://www.catalog.org">

   <complexType name="Person">
      <sequence>
         <element name="Name" type="string"/>
      </sequence>
      <attribute name="id" type="ID" use="required"/>
   </complexType>

   <complexType name="Author">
      <attribute name="idref" type="IDREF" use="required"/>
   </complexType>

   <complexType name="Book">
      <sequence>
         <element name="Title" type="string"/>
         <element name="Author" type="cat:Author"/>
      </sequence>
   </complexType>

   <complexType name="Catalog">
      <sequence>
         <element name="Person" type="cat:Person"/>
         <element name="Book" type="cat:Book"/>
      </sequence>
   </complexType>
   
   <element name="Catalog" type="cat:Catalog"/>
</schema>

******************************************************
Venetian Blind Design/Hide Namespaces/Instance Document
******************************************************

<?xml version="1.0"?>
<cat:Catalog xmlns:cat="http://www.catalog.org"
         xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
         xsi:schemaLocation=
                    "http://www.catalog.org
                     Catalog.xsd">
   <Person id="rbach">
      <Name>Richard Bach</Name>
   </Person>
   <Book>
      <Title>Illusions</Title>
      <Author idref="rbach"/>
   </Book>
</cat:Catalog>





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS