XML Schema — Part II

XML Foundations (INFOSYS 242)

Erik Wilde, UC Berkeley iSchool
Thursday, October 5, 2006
Creative Commons License

This work is licensed under a Creative Commons
Attribution-NonCommercial-ShareAlike 2.5 License.

Abstract

XML Schema allows greater flexibility in defining constraints on intra-document references than the ID/IDREF construct of DTDs. XML Schema's Identity Constraints are scoped, typed, and can be used for elements or attributes. The second aspect of XML Schema discussed today is the derivation of complex types. Complex types can be derived by restriction or extension. Complex type restriction defines the restricted type to be a more restricted version of the base type. Complex type extension make it possible to extend the base type by either adding attributes or contents (only by appending new content to the content model).

Outline (Local and Global Definitions)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Named and Anonymous Types

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

Outline (Elements)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Local vs. Global Elements

 <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"/>

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>

Outline (Attributes)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Attribute Definitions

Reusing Attributes

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>

Outline (Names and Namespaces)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Definitions

Instances

<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Multicolumn Layout in HTML</title>
  <style type="text/css">

Name Qualification

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/" elementFormDefault="qualified" attributeFormDefault="unqualified">

Outline (Identity Constraints)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Element = Type + Constraints

Improvements over ID/IDREF

Types of Identity Constraints

Identity Constraint Definitions

Identity Constraint Evaluation

Advanced Identity Constraints

Outline (Complex Type Derivation)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Type Derivation

Outline (Complex Type Restriction)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Removing Choices

Complex Type Restriction (Example)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <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" minOccurs="0"/>
   <xs:choice>
    <xs:element name="state" type="xs:string"/>
    <xs:element name="canton" type="xs:string"/>
   </xs:choice>
   <xs:element name="zip" type="xs:decimal"/>
  </xs:sequence>
  <xs:attribute name="country" type="xs:NMTOKEN"/>
  <xs:attribute name="territory" type="xs:string" use="optional"/>
 </xs:complexType>
 <xs:complexType name="USaddressType">
  <xs:complexContent>
   <xs:restriction base="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:choice>
      <xs:element name="state" type="xs:string"/>
     </xs:choice>
     <xs:element name="zip" type="zipType"/>
    </xs:sequence>
    <xs:attribute name="country" type="xs:NMTOKEN"/>
    <xs:attribute name="territory" type="xs:string" use="prohibited"/>
   </xs:restriction>
  </xs:complexContent>
 </xs:complexType>
 <xs:simpleType name="zipType">
  <xs:restriction base="xs:decimal">
   <xs:totalDigits value="5"/>
  </xs:restriction>
 </xs:simpleType>
</xs:schema>

Processing Restricted Complex Types

Outline (Complex Type Extension)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Adding Content

Complex Type Extension (Example)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <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" minOccurs="0"/>
   <xs:choice>
    <xs:element name="state" type="xs:string"/>
    <xs:element name="canton" type="xs:string"/>
   </xs:choice>
   <xs:element name="zip" type="xs:decimal"/>
  </xs:sequence>
  <xs:attribute name="country" type="xs:NMTOKEN"/>
  <xs:attribute name="territory" type="xs:string" use="optional"/>
 </xs:complexType>
 <xs:complexType name="businessAddressType">
  <xs:complexContent>
   <xs:extension base="addressType">
    <xs:sequence>
     <xs:element name="company" type="xs:string"/>
     <xs:element name="position" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="relationship" type="xs:NMTOKEN"/>
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:schema>

Processing Extended Complex Types

Outline (Conclusions)

  1. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]

Schema Components

XML Schema Features