XML Schema


Einleitung

Drachen

Notwendigkeit von Datentypen

class Bestellung {
      Integer lfdNr;
      String bemerkung;
      Date bestellDatum;
      Date lieferDatum;
      Adresse rechnungsAdresse;
      Adresse lieferAdresse;
      Artikel[] artikel;
}

class Adresse {
      String vorName;
      String nachName;
      String strasse;
      Integer plz;
      String ort;
}

class Artikel { 
      Integer aNr;
      String bezeichnung;
      Float preis;
      Integer anzahl;
}

class Date { ... }

Wie können diese Informationen typsicher über das Internet ausgetauscht werden?

Mit XML ist nur die folgende Definition möglich.

<!ELEMENT Bestellung (lfdNr, bemerkung?, 
                      bestellDatum, lieferDatum,
                      rechnungsAdresse, lieferAdresse,
                      waren) >
<!ELEMENT lfdNr (#PCDATA) >
<!ELEMENT bemerkung (#PCDATA) >
<!ELEMENT bestellDatum (#PCDATA) >
<!ELEMENT lieferDatum (#PCDATA) >

<!ELEMENT rechnungsAdresse (vorName, nachName,
                            strasse, plz, ort) >
<!ELEMENT lieferAdresse (vorName, nachName,
                         strasse, plz, ort) >
<!ELEMENT vorName (#PCDATA) >
<!ELEMENT nachName (#PCDATA) >
<!ELEMENT strasse (#PCDATA) >
<!ELEMENT plz (#PCDATA) >
<!ELEMENT ort (#PCDATA) >

<!ELEMENT waren (artikel+) >
<!ELEMENT artikel (aNr, bezeichnung?,
                   preis?, anzahl) >
<!ELEMENT aNr (#PCDATA) >
<!ELEMENT bezeichnung (#PCDATA) >
<!ELEMENT preis (#PCDATA) >
<!ELEMENT anzahl (#PCDATA) >

Probleme mit dieser Definition:

folgendes fehlerhaftes Dokument lässt sich damit definieren:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Bestellung SYSTEM "Bestellung.dtd" >

<lfdNr>4711</lfdNr>
<bemerkung>eilig</bemerkung>
<bestellDatum>12.1.01</bestellDatum>
<lieferDatum>2001-01-30</lieferDatum>

<rechnungsAdresse>
<vorName>Karl</vorName>
<nachName>Dall</nachName>
<strasse>L 15, 16</strasse>
<plz>68131</plz>
<ort>Mannheim</ort>
</rechnungsAdresse>

<lieferAdresse>
<vorName>Karl</vorName>
<nachName>Knallinger</nachName>
<strasse>A 5, 6</strasse>
<plz>D-68131</plz>
<ort>Mannheim</ort>
</lieferAdresse>

<waren>
<artikel>
<aNr>4712</aNr>
<anzahl>zwei</anzahl>
</artikel>
<artikel>
<aNr>4713</aNr>
<anzahl>-3</anzahl>
</artikel>
</waren>

</Bestellung>

Eine XML Schema Definition mit streng typisierten Definitionen folgen im Abschnitt Beispiele

XML Schema Sprachkonstrukte: Structures

von XML zu Schema

in XML DTD:

<!ELEMENT ename (cname*,econtent) >
<!ATTLIST ename aname atyp awert >

XML Schema Definition:

<element name="ename" >   
<complexType>   
<sequence> 
<element name="cname" type="mytype" maxOccurs="5" />
econtent 
</sequence> 
<attribute name="aname" >
<simpleType type="atyp" value="awert" />
</attribute>
</complexType> 
</element> 

Namensräume

XML Schema Processor Conformance

XML Schema Datentypen (schema components)

XML Schema Facetten (schema facets)

Beschränkungen (contraints) der Datentypen

Schema

<schema
    xmlns="http://www.w3.org/2000/10/XMLSchema
    targetNamespace="http://purl.org/metadata/dublin_core"
    version="M.n"
  >
Content: ((include | import | redefine | annotation)* , 
         ((attribute | attributeGroup | 
           complexType | element | group | notation | simpleType),
          annotation*)*)
</schema>

Element

<element 
    default = string 
    fixed = string 
    maxOccurs = for maxOccurs : 1
    minOccurs = nonNegativeInteger : 1
    name = NCName 
    type = QName 
    ...
    >
Content: (annotation? , 
         ((simpleType | complexType)? , 
          (key | keyref | unique)*))
</element>

Attribute

<attribute 
    name = NCName 
    type = QName 
    use = prohibited | optional | required | default | fixed : optional
    value = string 
    ...
    >
Content: (annotation? , (simpleType?))
</attribute>

complexType

<complexType 
    mixed = boolean : false
    name = NCName 
    ...
    >
Content: (annotation? , 
          (simpleContent | complexContent | 
           ((group | all | choice | sequence)? , 
            ((attribute | attributeGroup)* ,
             anyAttribute?))
          ))
</complexType>

simpleType

<simpleType 
    name = NCName 
    ...
    >
Content: (annotation? , 
          ((list | restriction | union)))
</simpleType>

attributeGroup

<attributeGroup 
    name = NCName 
    ...
    >
Content: (annotation? , 
          ((attribute | attributeGroup)* ,
           anyAttribute?))
</attributeGroup>

group

<group 
    maxOccurs = for maxOccurs : 1
    minOccurs = nonNegativeInteger : 1
    name = NCName 
    ...
    >
Content: (annotation? , 
          (all | choice | sequence)?)
</group>

notation

<notation 
    name = NCName 
    public = A public identifier, per ISO 8879 
    system = uriReference 
    ...
    >
Content: (annotation?)
</notation>

annotation

<annotation>
Content: (appinfo | documentation)*
</annotation>

XML Schema Sprachkonstrukte: Datatypes

Datentypen Definition

name = NCName
variety = ( atomic | list | union )
[baseTypeDefinition]
[facets]
[fundamentalFacets]
[annotation]

Basis Datentypen und Facetten

 {base type definition}applicable {facets}
If {variety} is list, then
 [all datatypes]length, minLength, maxLength, enumeration
If {variety} is union, then
 [all datatypes]pattern, enumeration
else if {variety} is atomic, then
primitivestringlength, minLength, maxLength, pattern, enumeration
booleanpattern
floatpattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
doublepattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
decimalprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
timeDurationpattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
recurringDurationduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
binaryencoding, length, minLength, maxLength, pattern, enumeration
uriReferencelength, minLength, maxLength, pattern, enumeration
IDlength, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
IDREFlength, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
ENTITYlength, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
NOTATIONlength, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
QNamelength, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
derivedlanguagelength, minLength, maxLength, pattern, enumeration
IDREFSlength, minLength, maxLength, enumeration
ENTITIESlength, minLength, maxLength, enumeration
NMTOKENlength, minLength, maxLength, pattern, enumeration
NMTOKENSlength, minLength, maxLength, enumeration
Namelength, minLength, maxLength, pattern, enumeration
NCNamelength, minLength, maxLength, pattern, enumeration
integerprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
nonPositiveIntegerprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
negativeIntegerprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
longprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
intprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
shortprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
byteprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
nonNegativeIntegerprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
unsignedLongprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
unsignedIntprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
unsignedShortprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
unsignedByteprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
positiveIntegerprecision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
timeInstantduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
timeduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
timePeriodduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
dateduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
monthduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
yearduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
centuryduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
recurringDateduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive
recurringDayduration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive

Quelle: W3C, 2000

Beispiele

Beispiel Bestellung (einfach)

Einfache Schema Definition mit den Datentypen string, integer, decimal in Anlehnung an die XML DTD.

<?xml version="1.0" encoding="UTF-8"?>

<schema>

<element name="Bestellung">
<complexType>
<sequence>
<element name="lfdNr" type="string" />
<element name="bemerkung" type="string" />
<element name="bestellDatum" type="Date" />
<element name="lieferDatum" type="Date" />
<element name="rechnungsAdresse" type="Adresse" />
<element name="lieferAdresse" type="Adresse" />
<element name="waren" type="Waren" />
</sequence>
</complexType>
</element>


<complexType name="Adresse">
<sequence>
<element name="vorName" type="string" />
<element name="nachName" type="string" />
<element name="strasse" type="string" />
<element name="plz" type="integer" />
<element name="ort" type="string" />
</sequence>
</complexType>


<complexType name="Waren">
<sequence>
<element name="artikel" type="Artikel" maxOccurs="10" />
</sequence>
</complexType>

<complexType name="Artikel">
<sequence>
<element name="aNr" type="integer" />
<element name="bezeichnung" type="string" minOccurs="0" />
<element name="preis" type="decimal" minOccurs="0" />
<element name="anzahl" type="integer" />
</sequence>
</complexType>

<simpleType name="Date" base="string" />

</schema>

Beispiel-Dokument mit Fehlern.

<?xml version="1.0" encoding="UTF-8" ?>
<Bestellung xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation='Bestellung-1.xsd'>

<lfdNr>4711</lfdNr>
<bemerkung>eilig</bemerkung>
<bestellDatum>12.1.01</bestellDatum>
<lieferDatum>2001-01-30</lieferDatum>

<rechnungsAdresse>
<vorName>Karl</vorName>
<nachName>Dall</nachName>
<strasse>L 15, 16</strasse>
<plz>68131</plz>
<ort>Mannheim</ort>
</rechnungsAdresse>

<lieferAdresse>
<vorName>Karl</vorName>
<nachName>Knallinger</nachName>
<strasse>A 5, 6</strasse>
<plz>D-68131</plz>
<ort>Mannheim</ort>
</lieferAdresse>

<waren>
<artikel>
<aNr>4712</aNr>
<anzahl>zwei</anzahl>
</artikel>
<artikel>
<aNr>4713</aNr>
<anzahl>-3</anzahl>
</artikel>
</waren>

</Bestellung>

Gefundene Fehler:

valid Bestellung-1e.xml
[Error] Bestellung-1e.xml:22:20: Datatype error: 
        In element 'plz' : 'D-68131' is not a decimal..
[Error] Bestellung-1e.xml:29:23: Datatype error: 
        In element 'anzahl' : 'zwei' is not a decimal..
Bestellung-1e.xml: 995 ms (24 elems, 2 attrs, 34 spaces, 104 chars)

Die Datumsprobleme und die Anzahl -3 werden nicht entdeckt.

Beispiel Bestellung (streng)

Funktioniert noch nicht ganz: mit Oracle xmlschema ok, mit Apache xerces in alter Syntax

<?xml version="1.0" encoding="UTF-8" ?>

<Bestellung 
   xmlns="http://www.example.com/Bestellung"
   xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
   xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
   xsi:schemaLocation="http://www.example.com/Bestellung Bestellung-4.xsd"
   >

<lfdNr>4711</lfdNr>
<bemerkung>eilig</bemerkung>
<bestellDatum>12.1.01</bestellDatum>
<lieferDatum>2001-01-30</lieferDatum>

<rechnungsAdresse>
<vorName>Karl</vorName>
<nachName>Dall</nachName>
<strasse>L 15, 16</strasse>
<plz>68131</plz>
<ort>Mannheim</ort>
</rechnungsAdresse>

<lieferAdresse>
<vorName>Karl</vorName>
<nachName>Knallinger</nachName>
<strasse>A 5, 6</strasse>
<plz>68131</plz>
<ort>Mannheim</ort>
</lieferAdresse>

<waren>
<artikel>
<aNr>4712</aNr>
<anzahl>2</anzahl>
</artikel>
<artikel>
<aNr>4713</aNr>
<anzahl>-3</anzahl>
</artikel>
</waren>

</Bestellung>

Definition for Oracle xmlschema

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
        targetNamespace="http://www.example.com/Bestellung"
        xmlns:r="http://www.example.com/Bestellung"
	elementFormDefault="qualified"
        >

<element name="Bestellung">
<complexType>
<sequence>
<element name="lfdNr" type="string" />
<element name="bemerkung" type="string" />
<element name="bestellDatum" type="r:Datum" />
<element name="lieferDatum" type="r:Datum" />
<element name="rechnungsAdresse" type="r:Adresse" />
<element name="lieferAdresse" type="r:Adresse" />
<element name="waren" type="r:Waren" />
</sequence>
</complexType>
</element>


<complexType name="Adresse">
<sequence>
<element name="vorName" type="string" />
<element name="nachName" type="string" />
<element name="strasse" type="string" />
<element name="plz" type="decimal" />
<element name="ort" type="string" />
</sequence>
</complexType>


<complexType name="Waren">
<sequence>
<element name="artikel" type="r:Artikel" maxOccurs="10" />
</sequence>
</complexType>

<complexType name="Artikel">
<sequence>
<element name="aNr" type="decimal" />
<element name="bezeichnung" type="string" minOccurs="0" />
<element name="preis" type="decimal" minOccurs="0" />
<element name="anzahl" type="positiveInteger" />
</sequence>
</complexType>

<simpleType name="Datum" >
<restriction base="string">
<pattern value="\d{4}-\d{2}-\d{2}" />
</restriction>
</simpleType>

</schema>

Gefundene Fehler (Oracle xmlschema):

valido Bestellung-4.xsd Bestellung-4e.xml
Parsing Bestellung-4e.xml

<Line 16, Column 37>: XSD-2025: (Error) 
    Invalid text '12.1.01' in element: 'bestellDatum'
<Line 42, Column 20>: XSD-2025: (Error) 
    Invalid text '-3' in element: 'anzahl'
Parser Exception: Invalid text '12.1.01' in element: 'bestellDatum'

Gefundene Fehler (Apache xerces):

valid Bestellung-3e.xml
[Error] Bestellung-3e.xml:7:38: Datatype error: 
        In element 'bestellDatum' : Value '12.1.01' does not 
        match regular expression facet '\d{4}-\d{2}-\d{2}'..
[Error] Bestellung-3e.xml:33:21: Datatype error: 
        In element 'anzahl' : -3 is out of bounds:[ 0 <= X    ]..
Bestellung-3e.xml: 1155 ms (24 elems, 2 attrs, 34 spaces, 99 chars)

Beispiel Personaldaten

xml, dtd, xsd

Stand der Entwicklung

11. Januar 2001


© Universität Mannheim, Rechenzentrum, 1998-2001.

Heinz Kredel
Last modified: Tue Feb 20 20:44:54 MET 2001