XML Schema (XSD) – Part II

XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)

Erik Wilde, UC Berkeley School of Information
2013-10-02

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: XML Schema (XSD) – Part II

Contents

E. Wilde: XML Schema (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. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  4. Conclusions [1]
Complex Types E. Wilde: XML Schema (XSD) – Part II

(4) What is a Complex Type?



Complex Types E. Wilde: XML Schema (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: XML Schema (XSD) – Part II

(6) Complex Types & Content Types

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


Content Models

Content Models E. Wilde: XML Schema (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: XML Schema (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: XML Schema (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. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  4. Conclusions [1]
XSD Design Patterns E. Wilde: XML Schema (XSD) – Part II

(12) Global Elements? Named Types?



XSD Design Patterns E. Wilde: XML Schema (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

Russian Doll E. Wilde: XML Schema (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: XML Schema (XSD) – Part II

(16) Russian Doll Structure

Russian Doll XSD

Venetian Blinds

Venetian Blinds E. Wilde: XML Schema (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: XML Schema (XSD) – Part II

(19) Venetian Blinds Structure

Venetian Blinds XSD

Salami Slice

Salami Slice E. Wilde: XML Schema (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: XML Schema (XSD) – Part II

(22) Salami Slice Structure

Salami Slice XSD

Garden of Eden

Garden of Eden E. Wilde: XML Schema (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: XML Schema (XSD) – Part II

(25) Garden of Eden Structure

Garden of Eden XSD

Local and Global Definitions

Outline (Local and Global Definitions)

  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. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  4. Conclusions [1]
Local and Global Definitions E. Wilde: XML Schema (XSD) – Part II

(27) Named and Anonymous Types

<!ELEMENT person (name, address) >
<!ATTLIST person id ID #REQUIRED >


Elements

Elements E. Wilde: XML Schema (XSD) – Part II

(29) Local vs. Global Elements

  • Elements can be defined in a type or in the schema
    • local elements can only be used where they are defined
    • global elements can be reused, they can serve as building blocks
  • Elements and complex types depend on each other
    • an element is defined by a type, often this will be a complex type
    • a complex type is defined by its contents, which are elements and/or attributes
 <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>
 <xs:element name="b" type="xs:string"/>


Elements E. Wilde: XML Schema (XSD) – Part II

(30) Reusable Elements

<?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>


Attributes

Attributes E. Wilde: XML Schema (XSD) – Part II

(32) Attribute Definitions

  • DTDs treat attributes as something entirely different from element content
    • they are defined in an ATTLIST, not in the ELEMENT definition
      <!ELEMENT person (name, address) >
      <!ATTLIST person id ID #REQUIRED >
    • they have a special range of Attribute Types [Document Type Definition (DTD); Attribute Types (1)] as opposed to elements
      <!ATTLIST person id ID #REQUIRED >
  • XSD overcomes these restrictions only partially
    • Simple Types [XML Schema (XSD) – Part I; Simple Types (1)] are used to define attribute (or element) contents
    • attributes are still described as something entirely different from an element's content model
  • Attributes could be better integrated into the model
    • RELAX NG [RELAX NG] treats attributes as part of an element's content model
    • this makes it trivial to have choices of element content and attributes


Attributes E. Wilde: XML Schema (XSD) – Part II

(33) Reusing Attributes

  • DTDs treat attributes as something local to an element
    • attributes are defined in an element's ATTLIST
    • reusing attributes for more than on element requires Parameter Entities [Document Type Definition (DTD); Parameter Entities (1)]
  • XSD better supports reuse of schema components
    • types can be defined locally (anonymous) or globally (named)
    • elements and attributes can be defined globally or locally
  • Globally defined attributes can be reused
    • the attribute definition does not tie it to any occurrence
    • the attribute can then be referenced from an complex type definition


Attributes E. Wilde: XML Schema (XSD) – Part II

(34) Reusing Attributes (Example)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <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>
 <xs:element name="b" type="xs:string"/>
 <xs:attribute name="class">
  <xs:simpleType>
   <xs:restriction base="xs:string">
    <xs:enumeration value="comment"/>
    <xs:enumeration value="warning"/>
   </xs:restriction>
  </xs:simpleType>
 </xs:attribute>
</xs:schema>


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. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  4. Conclusions [1]
Conclusions E. Wilde: XML Schema (XSD) – Part II

(36) Typed XML Structures



2013-10-02 XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)