XML Schema – structures

References for subject 2

Recommendations:

Tutorials and hints:

Defining dcuments structure 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 to 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 element content defined by this 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:complexType name="TextContent" mixed="true">
   <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="b"/>
      <xs:element ref="i"/>
   </xs:choice>
</xs:complexType>

In Schema we can control the order and number of subelements (which is rarely used, in fact):

<xs:complexType name="LetterBody" mixed="true">
   <xs:sequence>
      <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:choice>
      <xs:element name="signature" type="xs:string"/>
      <xs:element name="postscriptum" type="xs:string" minOccurs="0" maxOccurs="3"/>
   </xs:sequence>
</xs:complexType>

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.

Your project

Task 3.

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