XSD – Part III

XML Foundations [./]
Fall 2011 — INFO 242 (CCN 42596)

Ray Larson, UC Berkeley School of Information
2011-09-13

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 R. Larson: XSD – Part III

Contents

R. Larson: 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.



Local and Global Definitions

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 [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]
Local and Global Definitions R. Larson: XSD – Part III

(4) Named and Anonymous Types

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


Elements

Elements R. Larson: XSD – Part III

(6) 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 R. Larson: XSD – Part III

(7) 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 R. Larson: XSD – Part III

(9) 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 [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 [http://dret.net/lectures/xml-fall08/schemalanguages#%287%29] treats attributes as part of an element's content model
    • this makes it trivial to have choices of element content and attributes


Attributes R. Larson: XSD – Part III

(10) 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 R. Larson: XSD – Part III

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


Names and Namespaces

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 [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]
Names and Namespaces R. Larson: XSD – Part III

(13) Definitions



Names and Namespaces R. Larson: XSD – Part III

(14) Instances

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


Names and Namespaces R. Larson: XSD – Part III

(15) 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. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]
Identity Constraints R. Larson: XSD – Part III

(17) Element = Type + Constraints



Identity Constraints R. Larson: XSD – Part III

(18) Improvements over ID/IDREF



Identity Constraints R. Larson: XSD – Part III

(19) Types of Identity Constraints



Identity Constraints R. Larson: XSD – Part III

(20) Identity Constraint Definitions



Identity Constraints R. Larson: XSD – Part III

(21) Identity Constraint Evaluation

identity-constraints.png

Identity Constraints R. Larson: XSD – Part III

(22) Advanced Identity Constraints

identity-constraints++.png

Complex Type Derivation

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 [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]
Complex Type Derivation R. Larson: XSD – Part III

(24) Types in XSD



Complex Type Derivation R. Larson: XSD – Part III

(25) Type Derivation



Complex Type Restriction

Complex Type Restriction R. Larson: XSD – Part III

(27) 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 R. Larson: XSD – Part III

(28) 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 R. Larson: XSD – Part III

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

Complex Type Extension R. Larson: XSD – Part III

(31) 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 R. Larson: XSD – Part III

(32) 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 R. Larson: XSD – Part III

(33) 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. Local and Global Definitions [6]
    1. Elements [2]
    2. Attributes [3]
  2. Names and Namespaces [3]
  3. Identity Constraints [6]
  4. Complex Type Derivation [8]
    1. Complex Type Restriction [3]
    2. Complex Type Extension [3]
  5. Conclusions [2]
Conclusions R. Larson: XSD – Part III

(35) XSD Features



Conclusions R. Larson: XSD – Part III

(36) Schema Components

Schema Components

2011-09-13 XML Foundations [./]
Fall 2011 — INFO 242 (CCN 42596)