Dealing with <memo> documents

Figure 687. <memo> exercise series Slide presentation Create comment in forum
<memo xmlns:xsi=""
            xsi:noNamespaceSchemaLocation="memo.xsd" >
    <from>M. Goik</from>
    <to>B. King</to>
    <to>A. June</to>
    <subject>Best whishes</subject>
    <content>Hi all, congratulations to your splendid party</content>

exercise No. 6

Validation of memo document instances. Create comment in forum


Use OxygenXml for validation of message.xml against memo.xsd. Then subsequently do and undo the following changes each time checking for validity:

  • Omit the <from> element.

  • Change the order of the two sub elements <subject> and <content>.

  • Erase the date attribute among with its value.

  • Erase the priority attribute among with its value.

What do you observe?


The priority attribute is declared as optional and may thus be omitted. Erasing the priority attribute thus leaves the document in a valid state. The remaining three edit actions yield an invalid document instance.

A memo implementation sketch in Java

This exercises aim is about clarifying the (abstract) relation between XML schema's and assemblies of related Java classes. It's not about actually building a running application.

Model the <memo> schema as a set of Java classes as closely as possible. Do not supply any actual functionality.


The XML attributes date and priority can be mapped as Java instance attributes. The same applies to the elements <from>, <subject> and <content> which may be implemented as simple String instances. A simple sketch reads:

import java.util.Date;
import java.util.SortedSet;

public class Memo {
  private Date date;
  Priority priority = Priority.standard;
  private String from, subject,content;
  private SortedSet<String> to;
  // Accessors not yet implemented

The only point worth noting here is the <to> element's implementation: We want to be able addressing a set of recipients. Thus we have to disallow duplicates. Note that this is an informal constraint not being handled by our schema:

  • A <memo> document instance may contain duplicate content in its <to> nodes.

    This is a weakness of our current schema: We are yet unable to impose uniqueness constraints on partial document nodes set's content. This topic is being addressed in the section called “Uniqueness Constraints”.

  • Our list of recipients requires ordering: In a XML document instance the order of <to> nodes is important and hence must be preserved in a corresponding Java representation.

We thus choose a java.util.SortedSet<String> fulfilling both requirements.

We now consider a <memo>'s priority attribute:

<xs:simpleType name="Priority">
      <xs:restriction base="xs:string">
         <xs:enumeration value="low"/>
         <xs:enumeration value="medium"/>
         <xs:enumeration value="high"/>

Starting from Java 1.5 type safe enumerations are a perfect match:

public enum Priority {low, standard, high};

In the following chapters we will extend our <memo> schema demonstrating various concepts. In parallel a series of exercises deals with building a schema to be used for editing books. The latter gets extended step by step as our XML knowledge advances. We start with an initial exercise: