### Dealing with <memo> documents

No. 6

#### Validation of memo document instances.

 Q: 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  element. Change the order of the two sub elements  and . Erase the date attribute among with its value. Erase the priority attribute among with its value. What do you observe? A: 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  schema as a set of Java™ classes as closely as possible. Do not supply any actual functionality. A: The XML attributes date and priority can be mapped as Java™ instance attributes. The same applies to the elements ,  and  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 to; // Accessors not yet implemented }The only point worth noting here is the  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  document instance may contain duplicate content in its  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  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 's priority attribute:  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: