XSD – Part II

XML Foundations [./]
Fall 2010 — INFO 242 (CCN 42593)

Erik Wilde, UC Berkeley School of Information
2010-11-04

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents E. Wilde: XSD – Part II

Contents

E. Wilde: XSD – Part II

(2) Abstract

XSD Complex Types describe element content if this content is using attributes and/or element content other than only character data. Thus, complex types are used to define the allowed markup structures for a class of documents. Using XSD's type concepts, it is easier to represent model-level information in a schema, because type hierarchies can represent model-level specializations.



Complex Types

Outline (Complex Types)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
Complex Types E. Wilde: XSD – Part II

(4) What is a Complex Type?



Complex Types E. Wilde: XSD – Part II

(5) Complex Type Example

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="billingAddress" type="addressType"/>
 <xs:element name="shippingAddress" type="addressType"/>
 <xs:complexType name="addressType">
  <xs:sequence>
   <xs:element name="name" type="xs:string"/>
   <xs:element name="street" type="xs:string"/>
   <xs:element name="city" type="xs:string"/>
   <xs:element name="state" type="xs:string" minOccurs="0"/>
   <xs:element name="zip" type="xs:decimal"/>
  </xs:sequence>
  <xs:attribute name="country" type="xs:NMTOKEN"/>
 </xs:complexType>
</xs:schema>


Complex Types E. Wilde: XSD – Part II

(6) Complex Types & Content Types

Simple Types Complex Types
Simple Content Complex Content
Element Only Mixed Empty


Content Models

Outline (Content Models)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
Content Models E. Wilde: XSD – Part II

(8) DTD Content Models

  • Defining Elements [Document Type Definition (DTD); Defining Elements (1)] in DTDs uses a compact syntax
    • XSD supports the same facilities with a more verbose syntax
    • XSD adds features which DTDs do not support
  • DTDs allow elements to be mandatory, optional, repeatable, or optional and repeatable
    • XSD allows the cardinality to be specified
  • DTDs allow sequences (,) and alternatives (|)
    • XSD introduces a (very limited) operator for all groups
  • Apart from the syntax, XSD content models are not very different


Content Models E. Wilde: XSD – Part II

(9) Mixed Content

  • DTDs define mixed content by mixing #PCDATA into the content model
    • DTDs always require mixed content to use the form ( #PCDATA | a | b )*
    • the occurrence of elements in mixed content cannot be controlled
  • XSD defines mixed content outside of the content model
    • the content model is defined like an element-only content model
    • the mixed attribute on the type marks the type as being mixed
  • XSD mixed content can use all model groups
    • it is possible to constrain element occurrences in the same way as in element-only content
    • in practice, this feature is rarely used (mixed content often is very loosely defined)
 <xs:element name="p" type="mixedType"/>
 <xs:complexType name="mixedType" mixed="true">
  <xs:choice maxOccurs="unbounded" minOccurs="0">
   <xs:element ref="b"/>
   <xs:element name="i" type="xs:string"/>
   <xs:element name="u" type="xs:string"/>
  </xs:choice>
  <xs:attribute ref="class"/>
 </xs:complexType>


Content Models E. Wilde: XSD – Part II

(10) Empty Content

  • DTDs have a special keyword for empty elements
    • instead of the content model, the keyword EMPTY is used
    • empty elements may still have attribute lists associated with them
  • XSD empty types are defined implicitly
    • there is no explicit keyword for defining an empty type
    • if a type has no model group inside it, it is empty (it still may have attributes)


XSD Design Patterns

Outline (XSD Design Patterns)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
XSD Design Patterns E. Wilde: XSD – Part II

(12) Global Elements? Named Types?



XSD Design Patterns E. Wilde: XSD – Part II

(13) Sample XML

<?xml version="1.0" encoding="UTF-8"?>
<person>
 <name>
  <givenname>Erik</givenname>
  <givenname>Thomas</givenname>
  <surname>Wilde</surname>
 </name>
 <address>
  <company>ETH Zürich</company>
  <email>net.dret@dret.net</email>
 </address>
</person>


Russian Doll

Outline (Russian Doll)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
Russian Doll E. Wilde: XSD – Part II

(15) Russian Doll XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:element name="person">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="givenname" type="xs:token" maxOccurs="unbounded"/>
       <xs:element name="surname" type="xs:string"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    <xs:element name="address">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="company" type="xs:string"/>
       <xs:element name="email" minOccurs="0">
        <xs:simpleType>
         <xs:restriction base="xs:string">
          <xs:pattern value=".*@.*\..*"/>
         </xs:restriction>
        </xs:simpleType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>


Russian Doll E. Wilde: XSD – Part II

(16) Russian Doll Structure

Russian Doll XSD

Venetian Blinds

Outline (Venetian Blinds)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
Venetian Blinds E. Wilde: XSD – Part II

(18) Venetian Blinds XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:element name="person" type="personType"/>
 <xs:complexType name="personType">
  <xs:sequence>
   <xs:element name="name" type="nameType"/>
   <xs:element name="address" type="addressType"/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name="nameType">
  <xs:sequence>
   <xs:element name="givenname" type="givennameType" maxOccurs="unbounded"/>
   <xs:element name="surname" type="surnameType"/>
  </xs:sequence>
 </xs:complexType>
 <xs:simpleType name="givennameType">
  <xs:restriction base="xs:token"/>
 </xs:simpleType>
 <xs:simpleType name="surnameType">
  <xs:restriction base="xs:string"/>
 </xs:simpleType>
 <xs:complexType name="addressType">
  <xs:sequence>
   <xs:element name="company" type="companyType"/>
   <xs:element name="email" type="emailType" minOccurs="0"/>
  </xs:sequence>
 </xs:complexType>
 <xs:simpleType name="companyType">
  <xs:restriction base="xs:string"/>
 </xs:simpleType>
 <xs:simpleType name="emailType">
  <xs:restriction base="xs:string">
   <xs:pattern value=".*@.*\..*"/>
  </xs:restriction>
 </xs:simpleType>
</xs:schema>


Venetian Blinds E. Wilde: XSD – Part II

(19) Venetian Blinds Structure

Venetian Blinds XSD

Salami Slice

Outline (Salami Slice)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
Salami Slice E. Wilde: XSD – Part II

(21) Salami Slice XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:element name="person">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="name"/>
    <xs:element ref="address"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="name">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="givenname" maxOccurs="unbounded"/>
    <xs:element ref="surname"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="givenname" type="xs:token"/>
 <xs:element name="surname" type="xs:string"/>
 <xs:element name="address">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="company"/>
    <xs:element ref="email" minOccurs="0"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="company" type="xs:string"/>
 <xs:element name="email">
  <xs:simpleType>
   <xs:restriction base="xs:string">
    <xs:pattern value=".*@.*\..*"/>
   </xs:restriction>
  </xs:simpleType>
 </xs:element>
</xs:schema>


Salami Slice E. Wilde: XSD – Part II

(22) Salami Slice Structure

Salami Slice XSD

Garden of Eden

Outline (Garden of Eden)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
Garden of Eden E. Wilde: XSD – Part II

(24) Garden of Eden XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:element name="person" type="personType"/>
 <xs:element name="name" type="nameType"/>
 <xs:element name="givenname" type="givennameType"/>
 <xs:element name="surname" type="surnameType"/>
 <xs:element name="address" type="addressType"/>
 <xs:element name="company" type="companyType"/>
 <xs:element name="email" type="emailType"/>
 <xs:complexType name="personType">
  <xs:sequence>
   <xs:element ref="name"/>
   <xs:element ref="address"/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name="nameType">
  <xs:sequence>
   <xs:element ref="givenname" maxOccurs="unbounded"/>
   <xs:element ref="surname"/>
  </xs:sequence>
 </xs:complexType>
 <xs:simpleType name="givennameType">
  <xs:restriction base="xs:token"/>
 </xs:simpleType>
 <xs:simpleType name="surnameType">
  <xs:restriction base="xs:string"/>
 </xs:simpleType>
 <xs:complexType name="addressType">
  <xs:sequence>
   <xs:element ref="company"/>
   <xs:element ref="email" minOccurs="0"/>
  </xs:sequence>
 </xs:complexType>
 <xs:simpleType name="companyType">
  <xs:restriction base="xs:string"/>
 </xs:simpleType>
 <xs:simpleType name="emailType">
  <xs:restriction base="xs:string">
   <xs:pattern value=".*@.*\..*"/>
  </xs:restriction>
 </xs:simpleType>
</xs:schema>


Garden of Eden E. Wilde: XSD – Part II

(25) Garden of Eden Structure

Garden of Eden XSD

Conclusions

Outline (Conclusions)

  1. Complex Types [6]
    1. Content Models [3]
  2. XSD Design Patterns [10]
    1. Russian Doll [2]
    2. Venetian Blinds [2]
    3. Salami Slice [2]
    4. Garden of Eden [2]
  3. Conclusions [1]
Conclusions E. Wilde: XSD – Part II

(27) Typed XML Structures



2010-11-04 XML Foundations [./]
Fall 2010 — INFO 242 (CCN 42593)