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>

complexContent

<complexContent 
    id = ID 
    mixed = boolean 
    ...
    >
Content: (annotation?, 
         (restriction | extension))
</complexContent>

simpleContent

<simpleContent 
    id = ID 
    ...
    >
Content: (annotation?, 
         (restriction | extension))
</simpleContent>

restriction

<restriction 
    base = QName 
    id = ID 
    ...
    >
simpleContent: (annotation?, 
  (simpleType?, 
  (minExclusive | minInclusive | maxExclusive | maxInclusive 
  | totalDigits | fractionDigits | length | minLength 
  | maxLength | enumeration | whiteSpace | pattern)*)?, 
  ((attribute | attributeGroup)*, anyAttribute?))

compelexContent: (annotation?, 
  (group | all | choice | sequence)?, 
  ((attribute | attributeGroup)*, anyAttribute?))
</restriction>

extension

<extension 
    base = QName 
    id = ID 
    ...
    >
simpleContent: (annotation?, 
  ((attribute | attributeGroup)*, anyAttribute?))

complexContent: (annotation?, 
  ((group | all | choice | sequence)?, 
  ((attribute | attributeGroup)*, anyAttribute?)))
</extension>

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"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

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


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


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

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


<xsd:simpleType name="Date" >
<xsd:restriction base="xsd:string" />
</xsd:simpleType>

</xsd: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>

Ohne Fehler:

valid  Bestellung-1e.xml
Bestellung-1e.xml: 1225 ms (24 elems, 0 attrs, 34 spaces, 104 chars)

schema validiation script:

#!/bin/sh
VALIDPATH="/home/kredel/java/lib/xmlParserAPIs.jar:/home/kredel/java/lib/xercesImpl.jar:/home/kredel/java/lib/xercesSamples.jar"
export CLASSPATH="$VALIDPATH:$CLASSPATH"
/usr/lib/jdk1.3/bin/java sax.Counter -v -s $* 

Gefundene Fehler:

schema  Bestellung-1e.xsi
[Error] Bestellung-1e.xsi:15:19: cvc-type.3.1.3: 
        The value 'D-68131' of element 'plz' is not valid.
[Error] Bestellung-1e.xsi:30:22: cvc-type.3.1.3: 
        The value 'zwei' of element 'anzahl' is not valid.
Bestellung-1e.xsi: 1383 ms (24 elems, 1 attrs, 0 spaces, 138 chars)

Die Datumsprobleme und die Anzahl -3 werden nicht entdeckt.

Beispiel Bestellung (streng)

Alle Beispiele für Apache xerces in aktueller Syntax

Verwendung von date

<?xml version="1.0" encoding="UTF-8" ?>
<Bestellung xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation='Bestellung-2.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>
<preis>DM 22</preis>
<anzahl>2</anzahl>
</artikel>
<artikel>
<aNr>4713</aNr>
<anzahl>3</anzahl>
</artikel>
</waren>

</Bestellung>

Schema Definition:

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

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

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

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

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

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

<xsd:simpleType name="Date" >
<xsd:restriction base="xsd:date" />
</xsd:simpleType>

</xsd:schema>

Gefundene Fehler (Apache xerces):

schema  Bestellung-2e.xsi
[Error] Bestellung-2e.xsi:7:37: cvc-type.3.1.3: 
        The value '12.1.01' of element 'bestellDatum' is not valid.
[Error] Bestellung-2e.xsi:29:21: cvc-type.3.1.3: 
        The value 'DM 22' of element 'preis' is not valid.
Bestellung-2e.xsi: 1402 ms (25 elems, 1 attrs, 0 spaces, 138 chars)

Verwendung von pattern und nonNegativeInteger

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

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

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

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

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

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

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

</xsd:schema>

Gefundene Fehler (Apache xerces):

schema  Bestellung-3e.xsi
[Error] Bestellung-3e.xsi:7:37: cvc-type.3.1.3: 
        The value '12.1.01' of element 'bestellDatum' is not valid.
[Error] Bestellung-3e.xsi:33:20: cvc-type.3.1.3: 
        The value '-3' of element 'anzahl' is not valid.
Bestellung-3e.xsi: 1479 ms (24 elems, 1 attrs, 0 spaces, 133 chars)

Definition von Elementen mit Text-Inhalt und Attribut.

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

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

<xsd:element name="Bestellung">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="lfdNr" type="xsd:string" />
<xsd:element ref="bemerkung" />
<xsd:element name="bestellDatum" type="Date" />
<xsd:element name="lieferDatum" type="Date" />
<xsd:element name="rechnungsAdresse" type="Adresse" />
<xsd:element name="lieferAdresse" type="Adresse" />
<xsd:element name="waren" type="Waren" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...
<xsd:element name="bemerkung" >
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string" >
<xsd:attribute name="priority" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>

</xsd:schema>

Beispiel Personaldaten

xml, dtd, xsd (veraltete Syntax)

Stand der Entwicklung

14. Januar 2002


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

Heinz Kredel
Last modified: Tue Jun 10 22:55:58 CEST 2003