XSD – Part IV

XML Foundations [./]
Fall 2008 — INFO 242 (CCN 42572)

Erik Wilde, UC Berkeley School of Information
2008-11-11

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 IV

Contents

E. Wilde: XSD – Part IV

(2) Abstract

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.



E. Wilde: XSD – Part IV

(3) Types in XSD



Complex Type Derivation

Outline (Complex Type Derivation)

  1. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  2. Conclusions [2]
Complex Type Derivation E. Wilde: XSD – Part IV

(5) Type Derivation



Complex Type Restriction

Outline (Complex Type Restriction)

  1. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  2. Conclusions [2]
Complex Type Restriction E. Wilde: XSD – Part IV

(7) 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: XSD – Part IV

(8) 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: XSD – Part IV

(9) 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. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  2. Conclusions [2]
Complex Type Extension E. Wilde: XSD – Part IV

(11) 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: XSD – Part IV

(12) 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: XSD – Part IV

(13) 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. Complex Type Derivation [7]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  2. Conclusions [2]
Conclusions E. Wilde: XSD – Part IV

(15) XSD Features



Conclusions E. Wilde: XSD – Part IV

(16) Schema Components

Schema Components

2008-11-11 XML Foundations [./]
Fall 2008 — INFO 242 (CCN 42572)