XML Schema – structures

References for subject 2


Tutorials and hints:

Defining the stucture of documents using XML Schema

See examples in XML02.zip. They show definition of the same document model written in several different ways. Note the differences in:

Try out validating the example document against different schemas (note the xsi:noNamespaceSchemaLocation attribute).

Task 1. Defining elements and complex types

Starting from students05 or higher, add definitions of new structures to the schema.

  1. Define room element – it has an identification number, number of seats, equipment (projector, blackboard, etc.)
  2. Define meeting element – each one has the following information specified: room, day of week, start-end hours.
  3. Add a sequence of meetings to the lecture.

Mixed content

Text mixed with embedded elements is called a mixed content. It is typical for the text-oriented applications of XML, e.g. XHTML.

Defining mixed content in XML Schema is very easy – we only need to set attribute mixed="true" in a complex type definition. It allows any text within an element mixed with the normal element content which is defined by the given complex type.

In order to reflect DTD-like definitions of complex content, which allow any number and order of subelements within mixed content, we have to describe something like choice*

Example 1. Defining complex type

Mixed content definition in DTD (the only allowed form):

<!ELEMENT p (#PCDATA | b | i)*>

The same definition in XML Schema:

<xs:element name="p">
   <xs:complexType mixed="true">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
         <xs:element ref="b"/>
         <xs:element ref="i"/>

In XML Schema we can control the order and number of subelements (which is rarely used, to be honest):

<xs:complexType name="LetterBody" mixed="true">
      <xs:element name="greeting" type="xs:string"/>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
         <xs:element name="important" type="xs:string"/>
         <xs:element name="quotation" type="xs:string"/>
      <xs:element name="signature" type="xs:string"/>
      <xs:element name="postscriptum" type="xs:string" minOccurs="0" maxOccurs="3"/>

Task 2. Mixed content (optional)

Allow embedded tags in text descriptions of lectures, as in the following fragment.

<description>Defining structure of XML documents (defining <term>XML applications</term>):
  DTD, <link href="http://www.w3.org/TR/xmlschema-0">XML Schema</link>,
  <link href="http://relaxng.org">Relax NG</link>.</description>					

Specify elements which have the following role:

  1. link – hyperlink to location specified in href attribute
  2. term – a term / name / keyword
  3. strong – strong emphasis
  4. code – code fragment

Allow the same type of content in item elements of lecture program.

Allow the elements to be embedded.

Deterministic / nondeterministic model

Task 3.

Try to write a schema definition for an element which may contain a sequence of alternating elements <a> and <b>. Allow the content to end with a or b.

Any problems? :)

XML Schema does not allow to define nondeterminicstic models of element content.

Some nondeterministric definitions can be rewritten to deterministic equivalents, but some models are not expressible in XML Schema at all!

Your project

Task 4.

Prepare an example document (or a fragment) related to your assessment project.

Start defining your schema and validating document fragments you have.

Valid XHTML 1.1Valid CSS