Element definitions

Figure 660. organizer.xsd XSD schema Slide presentation Create comment in forum
organizer.xsd myContacts.xml
<xs:element name="contact"> 
  <xs:complexType> 
    <xs:sequence> 
      <xs:element name="firstName"/> 
      <xs:element name="lastName"/>
      <xs:element name="email"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<contact
  xmlns:xsi=
    "http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="organizer.xsd" >
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>
  <email>dexter@company.com</email>
</contact>

Defining element <contact>.

A complexType indicator defines elements having an inner structure i.e. child elements and/or attributes.

A sequence of child elements. Alternatives:

  • <xs:choice>

  • <xs:all>

All three elements firstName, lastName and email must appear exactly once and in the given order.

Reference from XML instance to schema definition.


Figure 661. Linking instance to schema Slide presentation Create comment in forum
<contact
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:noNamespaceSchemaLocation  = "organizer.xsd"> 
  ...
</contact>

Binding the schema instance namespace http://www.w3.org/2001/XMLSchema-instance to the prefix xsi. From now on we may the prefix substitutes the namespace. The following two example definitions are thus equivalent:

  • <element xmlns="http://www.w3.org/2001/XMLSchema-instance" ... >

  • <xs:element ...>

Note

Choosing a prefix is a document local operation. The following example is thus completely equivalent:

<contact xmlns:dummy="http://www.w3.org/2001/XMLSchema-instance"
  dummy:noNamespaceSchemaLocation = "organizer.xsd">
  ...
</contact>

Selecting attribute noNamespaceSchemaLocation from namespace "http://www.w3.org/2001/XMLSchema-instance".

The root element <contact> is being defined in file organizer.xsd.


Figure 662. Centralized schema definition Slide presentation Create comment in forum
<contact
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="https://mi.hdm-stuttgart.de/organizer.xsd">
  ...
</contact>

Figure 663. Validating a contact Slide presentation Create comment in forum
> xmlparse -V  -s  myContacts.xml 
Parsing was successfull 

Active validation option: Do not just check for well - formedness.

Use XML Schema validation rather than legacy DTD validation.

The instance file to be validated. During parsing the xsi:noNamespaceSchemaLocation attribute will allow for analyzing the underlying schema.

The parser's result.


Figure 664. Uups, we fail! Slide presentation Create comment in forum
<contact xmlns:xsi=
   "http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="organizer.xsd">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>
  <!-- no <email> here -->
</contact>
The content of element 'contact' is not complete. One of '{email}' is expected.

Figure 665. Schema observations Slide presentation Create comment in forum
  • An XSD schema is itself an XML document.

  • XML documents may be valid with respect to a given XSD schema.

  • Compare multiple instances / single class relationship:

    // Multiple instances of class String
    String s1 = new String(), s2 = new String();
  • Machine readable, providing validation


Figure 666. Well-formed and valid documents Slide presentation Create comment in forum

Figure 667. Terminology Slide presentation Create comment in forum
  • Validating / non-validating: A non-validating parser checks for well- formedness. A validating parser in addition checks for schema compliance.

  • Valid / invalid documents: An XML document referencing a schema may either be valid or invalid with respect to the schema in question.

  • Document instance: If an XML document is valid with respect to a schema we call it an instance of the latter.


Figure 668. V02: Define <email> as optional Slide presentation Create comment in forum
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName"/>
      <xs:element name="lastName"/>
      <xs:element name="email" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<contact ...="organizer.xsd">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>
  <!-- no <email> required here -->
</contact>

Figure 669. V03: Multiple contacts Slide presentation Create comment in forum
<xs:element name="organizer">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="0" 
          maxOccurs="unbounded" 
          ref="contact"/> ━━━━┓ 
    </xs:sequence>            
  </xs:complexType>           
</xs:element>                 
<xs:element name="contact"> ◀━┛ 
  <xs:complexType>
    <xs:sequence>
        ...
      <xs:element name="email" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<organizer ...="organizer.xsd">
  <contact>
    <firstName>Eve</firstName>
    <lastName>Dexter</lastName>
  </contact>
  <contact>
    <firstName>Joe</firstName>
    <lastName>Simpson</lastName>
    <email>touching@the.void</email>
  </contact>
</organizer>

The minimum number of appearances is 0: We may have no <contact> element at all.

The maximum number of appearances is unlimited. Thus we may have an arbitrary number of <contact> elements.

Referencing definition of element <contact>.

Definition of element contact being the reference target of .


Figure 670. Choose <contact> as root Slide presentation Create comment in forum
<xs:element name="organizer">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="contact"
        minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName"/>
      <xs:element name="lastName"/>
      <xs:element name="email" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<contact ...="organizer.xsd">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>
  <email>dexter@company.com</email>
</contact>

Rule: Every schema element may be chosen as an instance's root.


Figure 671. Graphical schema representation Slide presentation Create comment in forum
Graphical schema representation

Element <organizer>.

Element <organizer> is being composed of a sequence of other elements.

Multiplicity indicator like i.e. 0:n in relational database models.

Defining element <contact>: Notice the lower right arrow indicating a reference rather then an in-place definition.

<contact> being composed of a sequence of other elements again.

The leaf children of <contact>. Notice the different presentation weight of <email> in contrast do <firstName> and <lastName> indicating this element to be optional.


Figure 672. Adding <birthDate> Slide presentation Create comment in forum
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName"/>
      <xs:element name="lastName"/>
      <xs:element name="birthDate"
             minOccurs="0" maxOccurs="1"/>
      <xs:element name="email" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<contact ...="organizer.xsd">
  <firstName>Eve</firstName>
  <lastName>Dexter</lastName>

   <!-- valid though incomplete! -->
  <birthDate>1995-</birthDate>
</contact>

Figure 673. Supply a <birthDate> type Slide presentation Create comment in forum
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName"/>
      <xs:element name="lastName"/>
      <xs:element name="birthDate"
        type="xs:date"
        minOccurs="0" maxOccurs="1"/>
      <xs:element name="email"
          minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<organizer ...="organizer.xsd">
  <contact >
    ...
    <!-- '1995-11-39' is no valid value
        for 'date'. -->
    <birthDate>1995-11-39</birthDate>
  </contact>
  <contact>
    ...
    <!-- '21-st May 2004' is no valid
     value for 'date'. -->
    <birthDate>21-st May 2004</birthDate>
  </contact>

Figure 674. <birthDate> element or attribute? Slide presentation Create comment in forum
  • Zero or one <birthDate> per <contact>: No multivalued property.

  • No inner structure within <birthDate>.

Conclusion: Use a birthDate attribute rather than an element.