XML Schema, XSD, Notes

  1. Intro
    XML Schema is an XML based alternative to DTD
    An XML Schema describes the structure of an XML document
    The XML language is also referred to as XML Schema Definition (XSD)
  2. With support for data types:
    It's easier to describe permissible document content
    It's easier to validate data
    It's easier to work with data from a database
    It's easier to define restrictions on data
    It's easier to define data formats
    It's easier to convert data between data types
  3. XML Schemas use XML Syntax
    So you don't have to learn another language
    You can use the XML editor to edit the Schema files
    You can use the XML Parser to parse the Schema files
    You can manipulate the Schema with the XML DOM
    You can transform the Schema with XSLT
  4. XML Schemas Secure Data Communications
    The sender can describe the data in a way the receiver will understand
    Example:
                  <date type = "date">1999-03-11</date>
                
    This ensures an understanding of the content because the XML data type date
    Requires the format CCYY-MM-DD
  5. XML Schemas are Extensible
    Because they are written in XML
    So you can reuse them in other Schemas
    You can create your own data types derived from standard types
    You can reference multiple Schemas from the same document
  6. Requirements
  7. Examples:
  8. The <schema> Element
    The <schema> element is the root element of every XML Schema!
    The <schema> element can have attributes
  9. Simple XSD Elements
    A Simple Element is an XML Element that can contain only Text.
    It Cannot Contain Any other Elements or Attributes
    But it can be of any different type defined in the XML Schema Definition
    Or it can be of a type "User" defined
    You can also add restrictions, (facets), to a data type to limit it's content
    You can require the data to match a predefined pattern
  10. Common XML Schema Data Types
  11. Declaring Default and Fixed Values for Simple Elements
    Simple elements can have a default value or a fixed value set
    A default value is automatically assigned to the element
    when no other value is specified
    <xs:element name = "color" type = "xs:string" default = "red" />
    Fixed values are automatically assigned to the element and
    you cannot specify another value
    <xs:element name = "color" type = "xs:string" fixed = "red" />
  12. XSD Attributes
    All Attributes are declared as Simple Types
    Only Complex Elements can have Attributes
  13. Declaring Default and Fixed Values for Attributes
    Attributes can have a default value or a fixed value specified
    <xs:attribute name = "lang" type = "xs:string" default = "EN" />
    Fixed values are automatically assigned to the attribute
    xs:attribute name = "lang" type = "xs:string" fixed = "EN" />
  14. Creating Optional and Required Attributes
    All attributes are optional by default
    To specify optional or required use the "use" attribute
    <xs:attribute name = "lang" type = "xs:string" use = "optional" />
    <xs:attribute name = "lang" type = "xs:string" use = "required" />
  15. Restrictions/Facets
    Restrictions are used to control acceptable values for XML elements or attributes
    Restrictions on XML elements are called Facets
  16. XSD Complex Elements
    A Complex Element is an XML Element that contains other elements and/or attributes
  17. XSD Empty Complex Elements
    Empty complex elements can contain attributes but cannot have any content between the opening and closing tags
  18. XSD Complex Type Elements Only
    An "elements only" complex type contains an element that contains only other elements
    Example: An XML element "person" that contains only other elements
                <person>
                  <firstname>Doug</firstname>
                  <lastname>Allard</lastname>
                </person>
              
    The schema definition looks like this:
                <xs:element name = "person">
                  <xs:complexType>
                    <xs:sequenct>
                      <xs:element name = "firstname" type = "xs:string" />
                      <xs:element name = "lastname"  type = "xs:string" />
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              
    Notice that the <xs:sequence> tag means that the elements defined ("firstname" and "lastname") must appear in that order inside a "person" element
    Or you can give the complexType element a name and let the "person" element have a type attribute that refers to the name of the complexType
    (If you use this method, several elements can refer to the same complex type)
                <xs:element name = "person" type = "persontype" />
    
                <xs:complexType name = "pserontype">
                  <xs:sequence>
                    <xs:element name = "firstname" type = "xs:string" />
                    <xs:element name = "lsastname" type = "xs:string" />
                  </xs:sequence>
                </xs:complexType>
              
  19. XSD complex Text-Only Elements
    A complext text element can contain both attributes and text
    This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content.
    When using simple content, you must define an extension OR a restriction witin the simpleContent element... like this:
                <xs:element name = "somename">
                  <xs:complexType>
                    <xs:simpleContent>
                      <xs:extension base = "basetype">
                        ...
                        ...
                      </xs:extension>
                    </xs:simpleContent>
                  </xscomplexType>
                <xs:element>
              
    OR
                <xs:element name = "somename">
                  <xs:complexType>
                    <xs:simpleContent>
                      <xs:restriction base = "basetype">
                        ...
                        ...
                      </xs:restriction>
                    </xs:sompleContent>
                  </xs:complexType>
                </xs:element>
              
    Use the extension element to expand on the base simple type for the element and use the restriction element to limit the base simple type for the element
    Example
    An XML element "shoesize" that contains text-only
                <shoesize country = "france">35</shoesize>
              
    The following example declares a complexType "shoesize".
    The content is defined as an integer data type and the "shoesize" element also contains an attribute name "country"
                <xs:element name = "shoesize">
                  <xs:complexType>
                    <xs:simpleContent>
                      <xs:extension base = "xs:integer">
                        <xs:attribute name = "country type = "xs:string" />
                      </xs:extension>
                    </xs:simpleContent>
                  </xs:complexType>
                </xs:element>
              
    We could also give the comlexType element a name and let the "shoesize" element have a type attribute that refers to the name of the complexType
    If you use this method several elements can refer to the same complex type
                <xs:element name = "shoesize" type = "shoetype" />
    
                <xs:complexType name = "shoetype">
                  <xs:simpleContent>
                    <xs:extension base = "xs:integer">
                      <xs:attribute name = "country" type = "xs:string" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              
  20. XSD Complex Type With Mixed Content
    A mixed complex type element can contain attributes, elements and text.
  21. XSD Indicators
    7 types of Indicators to control how elements are to be used in documents
  22. XSD the <any> Element
    The <any> element is used to extend XML documents with elements not specified by the schema
    The following example is a fragment of an XML schema called "family.xsd".
    It shows a declaration for the "person" element.
    By using the <any> element you can extend, (after <lastname>), the content of "person" with any element:
              <xs:element name="person">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="firstname" type="xs:string"/>
                    <xs:element name="lastname" type="xs:string"/>
                    <xs:any minOccurs="0"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            
    Now we want to extend the "person" element with a "children" element.
    In this case we can do so even if the author of the schema above never declared any "children" element with an additonal schema file called "children.xsd"
              <?xml version="1.0" encoding="ISO-8859-1"?>
              <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
              targetNamespace="http://www.w3schools.com"
              xmlns="http://www.w3schools.com"
              elementFormDefault="qualified">
    
              <xs:element name="children">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="childname" type="xs:string"
                    maxOccurs="unbounded"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
    
              </xs:schema>
            
    The XML file below called "myfamily.xml" uses components from both schemas
              <?xml version="1.0" encoding="ISO-8859-1"?>
    
              <persons xmlns="http://www.microsoft.com"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:SchemaLocation="http://www.microsoft.com family.xsd
              http://www.w3schools.com children.xsd">
    
                <person>
                  <firstname>Hege</firstname>
                  <lastname>Refsnes</lastname>
                  <children>
                    <childname>Cecilie</childname>
                  </children>
                </person>
    
                <person>
                  <firstname>Stale</firstname>
                  <lastname>Refsnes</lastname>
                </person>
              </persons>
            
    The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element
    The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents
    They allow documents to contain additional elements that are not declard in the main XML schema
  23. XSD The <anyAttribute> Element
    The <anyAttribute> element is used to extend XML documents with attributes not specified by the schemas.
    The following fragment from an XML schema called "family.xsd" shows a declaration for the "person" element.
    By using the <anyAttribute> element you can add any number of attributes to the "person" element.
                <xs:element name="person">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="firstname" type="xs:string"/>
                      <xs:element name="lastname" type="xs:string"/>
                    </xs:sequence>
                    <xs:anyAttribute/>
                  </xs:complexType>
                </xs:element>
              
    The "gender" attribute is be declared in another schema file/dd>
                <?xml version="1.0" encoding="ISO-8859-1"?>
                <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://www.w3schools.com"
                xmlns="http://www.w3schools.com"
                elementFormDefault="qualified">
    
                <xs:attribute name="gender">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:pattern value="male|female"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:attribute>
    
                </xs:schema>
              
    And in the XML file, reference to both schema's will add the "gender" attribute onto the "person" element
                <?xml version="1.0" encoding="ISO-8859-1"?>
    
                <persons xmlns="http://www.microsoft.com"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:SchemaLocation="http://www.microsoft.com family.xsd
                  http://www.w3schools.com attribute.xsd">
                  <person gender="female">
                    <firstname>Hege</firstname>
                    <lastname>Refsnes</lastname>
                  </person>
                  <person gender="male">
                    <firstname>Stale</firstname>
                    <lastname>Refsnes</lastname>
                  </person>
                </persons>
              
  24. XSD Element Substitution
    Using the substitutionGroup attribute
    Example 1:
              <xs:element name = "name" type = "xs:string" />
              <xs:element name = "navn" substitutionGroup = "name" />
            
    In the above example the "name" element is the head element and the "navn" element is a substitution for "name".
    Example 2:
    XSD file
              <xs:element name="name" type="xs:string"/>
              <xs:element name="navn" substitutionGroup="name"/>
    
              <xs:complexType name="custinfo">
                <xs:sequence>
                  <xs:element ref="name"/>
                </xs:sequence>
              </xs:complexType>
    
              <xs:element name="customer" type="custinfo"/>
              <xs:element name="kunde" substitutionGroup="customer"/>
            
    Possible XML declarations
              <customer>
                <name>Doug Allard</name>
              </customer>
            
    Or
              <kunde>
                <navn>Doug Allard</navn>
              </kunde>
            
  25. Examples
    1. )
      Given the following XML "shiporder.xml"
                    <?xml version="1.0" encoding="ISO-8859-1"?>
      
                    <shiporder orderid="889923"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:noNamespaceSchemaLocation="shiporder.xsd">
                     <orderperson>John Smith</orderperson>
                     <shipto>
                      <name>Ola Nordmann</name>
                      <address>Langgt 23</address>
                      <city>4000 Stavanger</city>
                      <country>Norway</country>
                     </shipto>
                     <item>
                      <title>Empire Burlesque</title>
                      <note>Special Edition</note>
                      <quantity>1</quantity>
                      <price>10.90</price>
                     </item>
                     <item>
                      <title>Hide your heart</title>
                      <quantity>1</quantity>
                      <price>9.90</price>
                     </item>
                    </shiporder>
                  
      The XML document consists of a root element, "shiporder", that contains a required attribute called "orderid".
      The "shiporder" element contains 3 different child elements, "orderperson", "shipto", and "item".
      The "item" element appears twice and it contains a "title", an optional "note", a "quantity" and "price" elements.
      The line, xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", tells the XML parser that this document should be validated against a schema.
      The line, xsi:noNamespaceSchemaLocation="shiporder.xsd", specifies where the schema resides... here it is the same folder as "shiporder.xml".
      To create the Schema for the above XML by following the XML's structure
      1) Create a new file called "shiporder.xsd".
         Start the definition with the standard XML declaration
         followed by the xs:schema element used to define schemas.
                      <?xml version = "1.0" encoding = "ISO-8859-1" ?>
                      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      
                      ...
      
                      ...
      
                      </xs:schema>
                    
         In the schema above we use the standard namespace, xs, and the URI
         associated with this namespace is the Schema language definition,
         which has the standard value of http://www.w3.org/2001/XMLSchema.
      2) Define the "shiporder" element.
         This element has an attribute and contains other elements
         therefore it is a complex type.
         The child elements of "shiporder" are surrounded by an xs:sequence
         element that defines an ordered sequence of sub elements:
                      <xs:element name="shiporder">
                       <xs:complexType>
                        <xs:sequence>
                        ...
                        ...
                        </xs:sequence>
                        ...
                       </xs:complexType>
                      </xs:element>
                    
      3) Define the "orderperson" element.
         It is a simple type (no attributes or other elements).
         The type, xs:string, is prefixed with the namespace prefix
         associated with XML Schema indicating a predefined schema data type
                      <xs:element name = "orderperson" type = "xs:string" />
                    
      4) Define the "shipto" element.
         It is a complex element type
                      <xs:element name="shipto">
                       <xs:complexType>
                        <xs:sequence>
                         <xs:element name="name" type="xs:string"/>
                         <xs:element name="address" type="xs:string"/>
                         <xs:element name="city" type="xs:string"/>
                         <xs:element name="country" type="xs:string"/>
                        </xs:sequence>
                       </xs:complexType>
                      </xs:element>
                    
         With schemas we can define the number of possible occurrences for
         an element with the maxOccurs and the minOccurs attributes.
         The default value for both is 1
      5) Define the "item" element
         It is a complex type and can appear multiple times inside
         a "shiporder" element because maxOccurs is set to "unbounded".
         The "note" element is optional because minOccurs is zero
                      <xs:element name="item" maxOccurs="unbounded">
                       <xs:complexType>
                        <xs:sequence>
                         <xs:element name="title" type="xs:string"/>
                         <xs:element name="note" type="xs:string" minOccurs="0"/>
                         <xs:element name="quantity" type="xs:positiveInteger"/>
                         <xs:element name="price" type="xs:decimal"/>
                        </xs:sequence>
                       </xs:complexType>
                      </xs:element>
                    
      7) Declare the attribute "orderid" of the "shiporder" element.
         Since this is a required attribute we specify use="required".
         Note that the attribute declarations must always come last.
                      <xs:attribute name = "orderid" type = "xs:string" use="required" />
                    
      Here is the complete schema
                      <?xml version="1.0" encoding="ISO-8859-1" ?>
                      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      
                      <xs:element name="shiporder">
                       <xs:complexType>
                        <xs:sequence>
                         <xs:element name="orderperson" type="xs:string"/>
                         <xs:element name="shipto">
                          <xs:complexType>
                           <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="address" type="xs:string"/>
                            <xs:element name="city" type="xs:string"/>
                            <xs:element name="country" type="xs:string"/>
                           </xs:sequence>
                          </xs:complexType>
                         </xs:element>
                         <xs:element name="item" maxOccurs="unbounded">
                          <xs:complexType>
                           <xs:sequence>
                            <xs:element name="title" type="xs:string"/>
                            <xs:element name="note" type="xs:string" minOccurs="0"/>
                            <xs:element name="quantity" type="xs:positiveInteger"/>
                            <xs:element name="price" type="xs:decimal"/>
                           </xs:sequence>
                          </xs:complexType>
                         </xs:element>
                        </xs:sequence>
                        <xs:attribute name="orderid" type="xs:string" use="required"/>
                       </xs:complexType>
                      </xs:element>
      
                      </xs:schema>
                    
      Dividing the Schema can make it easier
      Define all elements and attributes first, then refer to them using the ref attribute
                      <?xml version="1.0" encoding="ISO-8859-1" ?>
                      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                      
                      <xs:element name="orderperson" type="xs:string"/>
                      <xs:element name="name" type="xs:string"/>
                      <xs:element name="address" type="xs:string"/>
                      <xs:element name="city" type="xs:string"/>
                      <xs:element name="country" type="xs:string"/>
                      <xs:element name="title" type="xs:string"/>
                      <xs:element name="note" type="xs:string"/>
                      <xs:element name="quantity" type="xs:positiveInteger"/>
                      <xs:element name="price" type="xs:decimal"/>
                      
                      <xs:attribute name="orderid" type="xs:string"/>
                      
                      <xs:element name="shipto">
                       <xs:complexType>
                        <xs:sequence>
                         <xs:element ref="name"/>
                         <xs:element ref="address"/>
                         <xs:element ref="city"/>
                         <xs:element ref="country"/>
                        </xs:sequence>
                       </xs:complexType>
                      </xs:element>
                      <xs:element name="item">
                       <xs:complexType>
                        <xs:sequence>
                         <xs:element ref="title"/>
                         <xs:element ref="note" minOccurs="0"/>
                         <xs:element ref="quantity"/>
                         <xs:element ref="price"/>
                        </xs:sequence>
                       </xs:complexType>
                      </xs:element>
      
                      <xs:element name="shiporder">
                       <xs:complexType>
                        <xs:sequence>
                         <xs:element ref="orderperson"/>
                         <xs:element ref="shipto"/>
                         <xs:element ref="item" maxOccurs="unbounded"/>
                        </xs:sequence>
                        <xs:attribute ref="orderid" use="required"/>
                       </xs:complexType>
                      </xs:element>
      
                      </xs:schema>
                    
      Or you can use Named Types to define classes or types which allows you to reuse element definitions. This is done by nameing the simpleTypes and complexTypes elements and then pointing to them with the type attribute of the element.
                      <?xml version="1.0" encoding="ISO-8859-1" ?>
                      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      
                      <xs:simpleType name="stringtype">
                       <xs:restriction base="xs:string"/>
                      </xs:simpleType>
      
                      <xs:simpleType name="inttype">
                       <xs:restriction base="xs:positiveInteger"/>
                      </xs:simpleType>
      
                      <xs:simpleType name="dectype">
                       <xs:restriction base="xs:decimal"/>
                      </xs:simpleType>
      
                      <xs:simpleType name="orderidtype">
                       <xs:restriction base="xs:string">
                        <xs:pattern value="[0-9]{6}"/>
                       </xs:restriction>
                      </xs:simpleType>
      
                      <xs:complexType name="shiptotype">
                       <xs:sequence>
                        <xs:element name="name" type="stringtype"/>
                        <xs:element name="address" type="stringtype"/>
                        <xs:element name="city" type="stringtype"/>
                        <xs:element name="country" type="stringtype"/>
                       </xs:sequence>
                      </xs:complexType>
      
                      <xs:complexType name="itemtype">
                       <xs:sequence>
                        <xs:element name="title" type="stringtype"/>
                        <xs:element name="note" type="stringtype" minOccurs="0"/>
                        <xs:element name="quantity" type="inttype"/>
                        <xs:element name="price" type="dectype"/>
                       </xs:sequence>
                      </xs:complexType>
      
                      <xs:complexType name="shipordertype">
                       <xs:sequence>
                        <xs:element name="orderperson" type="stringtype"/>
                        <xs:element name="shipto" type="shiptotype"/>
                        <xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
                       </xs:sequence>
                       <xs:attribute name="orderid" type="orderidtype" use="required"/>
                      </xs:complexType>
      
                      <xs:element name="shiporder" type="shipordertype"/>
      
                      </xs:schema>
                    
      The restriction element indicates that the datatype is derived from a W3C
      XML Schema namespace datatype so the fragment <xs:restriction base = "xs:string"> means that the value of the element or attribute must be a string value.
      The restriction element is more often used to apply restrictions on elements.
                      <xs:simpleType name="orderidtype">
                       <xs:restriction base="xs:string">
                        <xs:pattern value="[0-9]{6}"/>
                       </xs:restriction>
                      </xs:simpleType>
                    
      The above indicates that the value of the element or attribute must be a string and it must be exactly six characters in a row with values ranging from 0 to 9.
  26. XSD String Datatypes
    The String datatype can contain characters, line feeds, charriage returns, and tabs
                <xs:element name = "customer" type = "xs:string" />
              
    The corresponding XML element might look like this
                <customer>Doug Allard</customer>
              
    Or look like this
                <customer>    Doug Allard    </customer>
              
    The XML procesor will not modify the value of string datatypes
  27. XSD Date and Time Datatypes
    Date: specified by "CCYY-MM-DD"
    All components are required.
                <xs:element name = "start" type = "xs:date" />
              
    The XML element might look like this
                <start>2002-09-24</start>
              
  28. XSD Numeric Datatypes
  29. XSD Micsellaneious Datatypes
Hosted by www.Geocities.ws

1