Einleitung
XML Schema Sprachkonstrukte: Structure
XML Schema Sprachkonstrukte: Datatypes
Beispiele
Stand der Entwicklung
Ersatz und Erweiterung für XML DTD
(Neben-)bedingungen für Aufbau der Dokumentenstruktur
z.B. Anzahl bestimmter Elemente
Bedingungen über Datentypen für Elemente und Attribute
z.B. Integer, Float, URL
Objektorientierung und Vererbung
mehrere konkurrierende Ansätze:
XDR (XML-Data Reduced, BizTalk),
SOX (Schema for Object-Oriented XML),
Schematron,
DSD (Document Structure Description),
XML Schema
beim W3C im Candidate Recommendation Zustand
Beispiel in XML
<!ELEMENT KontoStand (#PCDATA) >und XML-Schema
<element name="KontoStand"> <simpleType type="float" /> </element>
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:
lfdNr, plz, aNr, anzahl
sind nur als Zeichenkette definiert,
nicht als Integer
preis
ist nur als Zeichenkette definiert,
nicht als Float
bestellDatum, lieferDatum
sind nur als Zeichenkette definiert,
nicht als echter normalisierter Datumstyp
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
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>
genauere Kontrolle über die Datentypen im Content
Bedingungen über die Anzahl und der Datentypen: Basis, Derived, eigene
Kontrolle über die Datentypen und Werte von Attributen
in der gleichen Weise wie für Elemente
XML Schema Namespaces
(Prefix oft xsd:
)
http://www.w3.org/2000/10/XMLSchema
und für XML-Schema Dokumente
(Prefix oft xsi:
)
http://www.w3.org/2000/10/XMLSchema-instance
URI für XML-Schema Definitionen
xsi:schemaLocation="url1 url2 url3 ..."
xsi:noNamespaceSchemaLocation="datei.name"
minimal
geht auch für Programme, die Validieren
representational
Validieren auch externe Schema
full
Validieren auch Schema im WWW
einfache / elementare Typen (simple types)
zusammengesetzte Typen (complex types)
Attribute (attributs)
Elemente (elements)
Attribut-Gruppen
Identity Constraints
key, keyref, unique
Modell Gruppen
Folgen (sequence), Conjunction (All-Listen),
Disjunction (Choice-Listen)
Notationen
public, system URI
Annotationen, Bemerkungen, Kommentare
Partikel
Wildcards
Beschränkungen (contraints) der Datentypen
length, minLength, maxLength
bei Listen und Zeichenketten
pattern, enumeration
fast überall
maxInclusive, maxExclusive,
minInclusive, minExclusive
bei Zahlen und Zeiten
precision, scale
bei Zahlen
duration, period
bei Zeiten
<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>
Root Element, enthält alle anderen Konstrukte
<element default = string fixed = string maxOccurs = for maxOccurs : 1 minOccurs = nonNegativeInteger : 1 name = NCName type = QName ... > Content: (annotation? , ((simpleType | complexType)? , (key | keyref | unique)*)) </element>
Element, wie in XML
Datentypen können per type
Attribut
referenziert werden
Datentypen können auch direkt im Content definiert werden
<attribute name = NCName type = QName use = prohibited | optional | required | default | fixed : optional value = string ... > Content: (annotation? , (simpleType?)) </attribute>
Im Content von Attributen können einfache Datentypen definiert werden
Datentypen können auch per type
Attribut
referenziert werden
<complexType mixed = boolean : false name = NCName ... > Content: (annotation? , (simpleContent | complexContent | ((group | all | choice | sequence)? , ((attribute | attributeGroup)* , anyAttribute?)) )) </complexType>
Definition von Datentypen, die komplexeren Content haben können (Folgen, Mengen, Selektionen)
und von Datentypen die Attribute haben können
<simpleType name = NCName ... > Content: (annotation? , ((list | restriction | union))) </simpleType>
Definition von einfachen Datentypen, die keine Attribute haben können mit einfachem Content
<attributeGroup name = NCName ... > Content: (annotation? , ((attribute | attributeGroup)* , anyAttribute?)) </attributeGroup>
zur Zusammenfassung von Attributen zu Gruppen
<group maxOccurs = for maxOccurs : 1 minOccurs = nonNegativeInteger : 1 name = NCName ... > Content: (annotation? , (all | choice | sequence)?) </group>
zur Zusammenfassung von Elementen zu Gruppen
<notation name = NCName public = A public identifier, per ISO 8879 system = uriReference ... > Content: (annotation?) </notation>
Definition von Bezeichnugen
<annotation> Content: (appinfo | documentation)* </annotation>
zur Dokumentation der Schema-Definitionen
name = NCName variety = ( atomic | list | union ) [baseTypeDefinition] [facets] [fundamentalFacets] [annotation]
Definition der primitiven, abgeleiteten und eigenen Datentypen
Einschränkung oder Erweiterung des Datentyps durch Facetten
Facetten sind:
Muster (pattern), Aufzählungen (enumeration),
Längenbeschränkungen, Wertebereichsbeschränkungen,
Genauigkeit bei Zahlen
{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 | ||
primitive | string | length, minLength, maxLength, pattern, enumeration |
boolean | pattern | |
float | pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
double | pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
decimal | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
timeDuration | pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
recurringDuration | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
binary | encoding, length, minLength, maxLength, pattern, enumeration | |
uriReference | length, minLength, maxLength, pattern, enumeration | |
ID | length, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
IDREF | length, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
ENTITY | length, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
NOTATION | length, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
QName | length, minLength, maxLength, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
derived | language | length, minLength, maxLength, pattern, enumeration |
IDREFS | length, minLength, maxLength, enumeration | |
ENTITIES | length, minLength, maxLength, enumeration | |
NMTOKEN | length, minLength, maxLength, pattern, enumeration | |
NMTOKENS | length, minLength, maxLength, enumeration | |
Name | length, minLength, maxLength, pattern, enumeration | |
NCName | length, minLength, maxLength, pattern, enumeration | |
integer | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
nonPositiveInteger | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
negativeInteger | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
long | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
int | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
short | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
byte | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
nonNegativeInteger | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
unsignedLong | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
unsignedInt | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
unsignedShort | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
unsignedByte | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
positiveInteger | precision, scale, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
timeInstant | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
time | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
timePeriod | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
date | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
month | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
year | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
century | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
recurringDate | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive | |
recurringDay | duration, period, pattern, enumeration, maxInclusive, maxExclusive, minInclusive, minExclusive |
Quelle: W3C, 2000
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.
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)
11. Januar 2001
XML Schema, Part 0 Primer, Part 1 Structures, Part 2 Datatypes,
W3C Candidate Recommendation, 24. October 2000
Tools:
Apache Xerces mit SAX und DOM Parser
Oracle xmlschema
© Universität Mannheim, Rechenzentrum, 1998-2001.
Heinz Kredel Last modified: Tue Feb 20 20:44:54 MET 2001