XSD summary

Posted by Monik, 21 October 2010.
XSD Semantic Web XML
Based on XSD: http://www.w3schools.com/Schema/default.asp

* example:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="to" type="xs:string"/>
                <xs:element name="from" type="xs:string"/>
                <xs:element name="heading" type="xs:string"/>
                <xs:element name="body" type="xs:string"/>
           </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
* elementFormDefault="qualified" indicates that all elements in the XML must be namespace qualified

* to reference an XSD in an XML document the main node has to have the following attributes:
  • xmlns="http://www.w3schools.com" - default namespace declaration
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - make XML Schema Instance namespace available
  • xsi:schemaLocation="http://www.w3schools.com location_of_your_schema.xsd" - location of the schema to be used with given namespace
Elements in XSD schema:
  • simple element (e.g. <xs:element name="to" type="xs:string"/> ) - can contain only text (i.e. boolean, string, date, etc.), you can also restrict its type; the most common types:
    - xs:string
    - xs:decimal
    - xs:integer
    - xs:boolean
    - xs:date
    - xs:time
    * you can also specify a default value for an element;
  • an attribute - is a simple element (e.g. <xs:attribute name="xxx" type="yyy"/> ) - you can specify a default or a fixed (cannot be changed) value of an attribute; to make an attribute required you have to specify it, by using use="required" attribute;
  • complex element - composed of other elements and/or text; can also contain empty elements; there are two ways of defining a complex type:
    <xs:element name="employee">
    <xs:complexType>
     <xs:sequence>
        <xs:element name="firstname"
                   type="xs:string"/>
     <xs:element name="lastname"
                   type="xs:string"/>
     </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="employee"
    type="personinfo"/>

    <xs:complexType name="personinfo">
    <xs:sequence>
     <xs:element name="firstname"
                 type="xs:string"/>
     <xs:element name="lastname"
                 type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    In case of second solution, several elements can refer to the same complex type, moreover, new complex types can be built on top of existing ones, in the following way:
    <xs:complexType name="fullpersoninfo">
    <xs:complexContent>
    <xs:extension base="personinfo">
    <xs:sequence>
    ... new elements ...
    </xs:sequence>
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>
  • empty element (i.e. with even no text inside):
    <xs:element name="product">
    <xs:complexType>
    </xs:complexType>
    </xs:element>
    * there are special ways to define a complex element: with text only, with other elements only, with both text and other elements - i nie widze w tym zadnej logiki; for more information on this see: http://www.w3schools.com/Schema/schema_complex_elements.asp
Facets (restrictions):
  • restrictions on simple types:
    <xs:element name="car">
    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:restriction_type value="..."/>
    ...
    <xs:restriction>
    </xs:simpleType>
    </xs:element>
    (more here: http://www.w3schools.com/Schema/schema_facets.asp)
Indicators:
  1. elements after "<xs:complexType>:
    * All - kazde dziecko musi sie pojawic przynajmniej raz, w dowolnej kolejnosci;
    * Choice - jedno z dzieci ma sie pojawic;
    * Sequence - wszystkie dzieci w podanej kolejnosci
  2. attributes in element's definition:
    * maxOccurs
    * minOccurs
    * "For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1"
    * you can use maxOccurs="unbounded" to have the element appearing unlimited number of times
  3. element groups and attribute groups (can be used among other elements/attributes):
    - this is a reference to groups of elements: <xs:group ref="persongroup"/>
    - and this is a reference to a group of attributes:
    <xs:element name="person">
    <xs:complexType>
    <xs:attributeGroup ref="personattrgroup"/>
    </xs:complexType>
    </xs:element>
    read more here: http://www.w3schools.com/Schema/schema_complex_indicators.asp
Other elements:
  • "any" element: <xs:any minOccurs="0"/>
  • "anyAttribute" element: <xs:anyAttribute/>
  • what is interesting, with usage of the two above we can create extensible documents; also we can make a document can use e.g. two schemas, one for main part, and another for subparts
Element substitution:
  • <xs:element name="name" type="xs:string">
    <xs:element name="navn" substitutiongroup="name">
    - now "navn" can be used instead of "name"; we can also block an element from others substituting it, by using: block="substitution" attribute within the original element; read more here: http://www.w3schools.com/Schema/schema_complex_subst.asp
Dividing and organizing the schema: http://www.w3schools.com/Schema/schema_example.asp (this is quite important...)

More about data types - read here: http://www.w3schools.com/Schema/schema_dtypes_string.asp

Comments


Comments: