XSD – Part III

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

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

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 III

Contents

E. Wilde: 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.



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. Conclusions [1]
Local and Global Definitions E. Wilde: XSD – Part III

(4) Named and Anonymous Types

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


Elements

Elements E. Wilde: 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 E. Wilde: 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 E. Wilde: 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 [Alternative Schema Languages – Schematron; RELAX NG (1)] 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: 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 E. Wilde: 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. Conclusions [1]
Names and Namespaces E. Wilde: XSD – Part III

(13) Definitions



Names and Namespaces E. Wilde: 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 E. Wilde: 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. Conclusions [1]
Identity Constraints E. Wilde: XSD – Part III

(17) Element = Type + Constraints



Identity Constraints E. Wilde: XSD – Part III

(18) Improvements over ID/IDREF



Identity Constraints E. Wilde: XSD – Part III

(19) Types of Identity Constraints



Identity Constraints E. Wilde: XSD – Part III

(20) Identity Constraint Definitions



Identity Constraints E. Wilde: XSD – Part III

(21) Identity Constraint Evaluation

identity-constraints.png

Identity Constraints E. Wilde: XSD – Part III

(22) Advanced Identity Constraints

identity-constraints++.png

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. Conclusions [1]
Conclusions E. Wilde: XSD – Part III

(24) XSD Features



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