XML Schema (XSD) – Part III

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

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

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 III

Contents

E. Wilde: XML Schema (XSD) – Part III

(2) Abstract

XSD allows greater flexibility in defining constraints on intra-document references than the ID/IDREF construct of DTDs. XSD's Identity Constraints are scoped, typed, and can be used for elements or attributes. They are more powerful that the DTD's limited ID/IDREF mechanism, but still lack sufficient generality to support a really wide set of model constraints to be expressed. XSD 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). Complex type derivation allows XSD to express type hierarchies of complex types, which can be aligned with more or less specialized code for processing instances of these types.



Names and Namespaces

Outline (Names and Namespaces)

  1. Names and Namespaces [3]
  2. Identity Constraints [6]
  3. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  4. Conclusions [2]
Names and Namespaces E. Wilde: XML Schema (XSD) – Part III

(4) Definitions



Names and Namespaces E. Wilde: XML Schema (XSD) – Part III

(5) Instances

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


Names and Namespaces E. Wilde: XML Schema (XSD) – Part III

(6) Name Qualification

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


Identity Constraints

Outline (Identity Constraints)

  1. Names and Namespaces [3]
  2. Identity Constraints [6]
  3. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  4. Conclusions [2]
Identity Constraints E. Wilde: XML Schema (XSD) – Part III

(8) Element = Type + Constraints



Identity Constraints E. Wilde: XML Schema (XSD) – Part III

(9) Improvements over ID/IDREF



Identity Constraints E. Wilde: XML Schema (XSD) – Part III

(10) Types of Identity Constraints



Identity Constraints E. Wilde: XML Schema (XSD) – Part III

(11) Identity Constraint Definitions



Identity Constraints E. Wilde: XML Schema (XSD) – Part III

(12) Identity Constraint Evaluation

identity-constraints.png

Identity Constraints E. Wilde: XML Schema (XSD) – Part III

(13) Advanced Identity Constraints

identity-constraints++.png

Complex Type Derivation

Outline (Complex Type Derivation)

  1. Names and Namespaces [3]
  2. Identity Constraints [6]
  3. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  4. Conclusions [2]
Complex Type Derivation E. Wilde: XML Schema (XSD) – Part III

(15) Types in XSD



Complex Type Derivation E. Wilde: XML Schema (XSD) – Part III

(16) Type Derivation



Complex Type Restriction

Outline (Complex Type Restriction)

  1. Names and Namespaces [3]
  2. Identity Constraints [6]
  3. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  4. Conclusions [2]
Complex Type Restriction E. Wilde: XML Schema (XSD) – Part III

(18) Removing Choices

  • Complex types usually allow variability
    • minOccurs and maxOccurs allow variability in occurrences
    • choice groups allow to choose between a number of alternatives
    • attributes may be flagged as use="optional"
    • simple types allow the individual values to use certain sets of values
  • Complex type restriction allows restrictions of all these variations
    • minOccurs and maxOccurs can be made more restrictive
    • alternatives can be removed from choice groups
    • optional attributes can flagged as use="required" or use="prohibited"
    • the simple types of values can be set to more restricted simple types
  • The technical way of defining restrictions is cumbersome
    • when the base type changes, the restricted type has to be fixed by hand


Complex Type Restriction E. Wilde: XML Schema (XSD) – Part III

(19) 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>


Complex Type Restriction E. Wilde: XML Schema (XSD) – Part III

(20) Processing Restricted Complex Types

  • Values of restricted types are values of the base types
    • type restriction is defined so that restricted type values are always base type values
    • code processing a type can be reused to process restricted types
  • If there is a well-designed type hierarchy, programming becomes easier
    • simple code can be written to handle the basic types
    • if required, more advanced code can be written for the restricted types
    • in many cases, restriction is more for validation than for processing
  • XSDs may even use abstract types
    • no element will ever use the addressType
    • concrete elements will only use restricted types
    • there can be code handling the addressType which handles all addresses


Complex Type Extension

Outline (Complex Type Extension)

  1. Names and Namespaces [3]
  2. Identity Constraints [6]
  3. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  4. Conclusions [2]
Complex Type Extension E. Wilde: XML Schema (XSD) – Part III

(22) Adding Content

  • Complex types are element content and attributes
    • extensions can add content, but only at the end of the base content
    • extensions can add attributes (order is not significant for attributes)
  • Adding content to existing content may not change the existing content
    • if the content is element only, it has to remain element only
    • if the content is mixed, is has to remain mixed
    • if the content is empty, it may become element only or mixed
    • the reason for these rules is that mixed is a global property of a type
  • Adding attributes simply adds these to the list of existing attributes
    • the added attributes may be optional or required


Complex Type Extension E. Wilde: XML Schema (XSD) – Part III

(23) 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>


Complex Type Extension E. Wilde: XML Schema (XSD) – Part III

(24) Processing Extended Complex Types

  • Values of extended types are not values of the base types
    • type extension adds content add/or attributes to a type
    • if content is added, it is always added at the end of the base type's content
  • If there is a well-designed type hierarchy, programming becomes easier
    • simple code can be written to handle the basic types
    • if that should handle extended types, it must be written to handle extensions
    • handling extensions can be as simple as skipping them
  • XSDs may even use abstract types
    • no element will ever use the addressType
    • concrete elements will only use extended types
    • code handling extended types can build on code handling the base type


Conclusions

Outline (Conclusions)

  1. Names and Namespaces [3]
  2. Identity Constraints [6]
  3. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  4. Conclusions [2]
Conclusions E. Wilde: XML Schema (XSD) – Part III

(26) XSD Features



Conclusions E. Wilde: XML Schema (XSD) – Part III

(27) Schema Components

Schema Components

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