Język Schema Po zrealizowaniu materiału student będzie w stanie Stworzyć formalny opis dokumentu Deklarować elementy i atrybuty Określić strukturę elementów w dokumencie Operować dostępnymi typami danych oraz definiować własne Powiązać dokument ze schematem 2 1
W3C http://www.w3.org/standards/xml/schema W3Schools Schema tutorial http://w3schools.com/schema/default.asp W3C Schema Validator http://www.w3.org/2001/03/webdata/xsv Altova procesor (walidacja oraz transformacja dokumentu ) http://www.altova.com/download_components.html Schema Validator on-line http://tools.decisionsoft.com/schemavalidate/ Understanding W3C Schema Complex Types http://www.xml.com/lpt/a/833 3 Badanie poprawności semantycznej dokumentu Schemat i jego składowe Powiązanie dokumentu ze schematem Deklaracja elementu i atrybutu Typy danych, wbudowane oraz definiowane przez użytkownika Określanie struktury elementów 4 2
5 Formalna specyfikacja dokumentu Zestaw reguł i zasad określających strukturę oraz zawartość dokumentu Dokument elementy, atrybuty, struktura dokumentu Schemat opis zawartości oraz struktury elementów, atrybutów 6 3
Dostępne języki W3C Schema Document Type Definition(DTD) Relax-NG Schematron 7 Dokument Schemat danych Zgodność ze schematem Parser Struktura poprawna Struktura niepoprawna Składnia niepoprawna Składnia poprawna Dalsze przetwarzanie 8 4
deklaracja xml element główny (schema) przestrzeń nazw deklaracje elementów i atrybutów definicje typów <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/schema"> <!-- zawartość schematu (deklaracje elementów, atrybutów, definicje typów) --> </xsd:schema> 9 <?xml version="1.0"?> <student> <nazwisko>brzęczyszczykiewicz</nazwisko> <imie>grzegorz</imie> <miasto>chrząszczyŝewoszyce</miasto> </student> <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/schema"> <xsd:element name="student"> <xsd:complextype> <xsd:sequence> <xsd:element name="nazwisko" type="xsd:string"/> <xsd:element name="imie" type="xsd:string"/> <xsd:element name="miasto" type="xsd:string"/> </xsd:sequence> </xsd:element> </xsd:schema> 10 5
Przedsiębiorstwo A Przedsiębiorstwo B Dokument Sprawdzenie zgodności ze schematem Sprawdzenie zgodności ze schematem Dokument Dane Schemat dokumentu Dane 11 12 6
Element Prosty Złożony Tylko tekst Tylko tekst i atrybuty Tekst, atrybuty, elementy potomne 13 Deklaracja elementu <xsd:element name= "imie" type="xsd:string"/> <xsd:element name="wzrost" type="xsd:integer"/> <xsd:element name="dataurodzenia" type="xsd:date"/> Dokument zgodny ze schematem <imie>ania</imie> <wzrost>172</wzrost> <dataurodzenia>1989-07-19</dataurodzenia> 14 7
Deklaracja atrybutu <xsd:attribute name="plec" type="xsd:string"/> <xsd:attribute name= "wlosy" type="xsd:string"/> Dokument zgodny ze schematem <imie plec="kobieta" wlosy="rude">ania</imie> 15 Typy danych Wbudowane Definiowane znakowy: string, normalizedstring, token, liczbowy: integer, decimal, float, double, logiczny: boolean daty i czasu: datetime, time, date, duration, Prosty (simple type) Złożony (complex type) 16 8
Utwórz nowy typ danych Zastosuj complextype nie Typ zawiera tak nie Możliwość tak element lub tworzenia atrybut? elementów? Zastosuj simpletype Dozwolona wartość tekstowa elementu Wykorzystaj restiction, list lub union Użyj simplecontent Dozwolona wartość tekstowa i atrybuty Wykorzystaj extension (tylko atryb.) lub restriction Użyj complexcontent Dozwolona wartość tekstowa, elementy potomne i atrybuty Wykorzystaj restriction lub extension Źródło: opracowanie własne na podstawie: http://www.xml.com/2001/08/22/examples/schematree.pdf 17 Definicja typu prostego <!-- element zawierający wyłącznie tekst --> <xsd:simpletype name="typnrdniatygodnia"> <xsd:restriction base="xsd:integer"> <xsd:mininclusive value="1"/> <xsd:maxinclusive value="7"/> </xsd:restriction> </xsd:simpletype> Deklaracja elementu <xsd:element name="nrdniatygodnia" type="typnrdniatygodnia"/> Dokument zgodny ze schematem <NrDniaTygodnia>4</NrDniaTygodnia> 18 9
Definicja typu złożonego <! element zawierający tekst i atrybut --> <xsd:complextype name="typstudent"> <xsd:simplecontent> <xsd:extension base="xsd:string"> <xsd:attribute name="wiek" type="xsd:integer" use="required"/> </xsd:extension> </xsd:simplecontent> Deklaracja elementu <xsd:element name="student" type="typstudent"/> Dokument zgodny ze schematem <student wiek="22">jan Kowalski</student> 19 Element z elementami potomnymi Dowolny już istniejący typ złożony lub anytype <!-- element z elementami potomnymi --> <xsd:complextype name="typstudent"> <xsd:complexcontent> <xsd:restriction base="xsd:anytype"> <xsd:sequence> <xsd:element name="nazwisko" type="xsd:string"/> <xsd:element name="rokstudiow" type="xsd:integer"/> </xsd:sequence> </xsd:restriction> </xsd:complexcontent> 20 10
Definicja typu złożonego (jeśli wywodzi się z anytype) <!-- definicja typu złoŝonego: element z elementami potomnymi --> <xsd:complextype name="typstudent"> <xsd:sequence> <xsd:element name="nazwisko" type="xsd:string"/> <xsd:element name="rokstudiow" type="xsd:integer"/> </xsd:sequence> Deklaracja elementu <xsd:element name="student" type="typstudent"/> Dokument zgodny ze schematem <student> <nazwisko>kowalski</nazwisko> <rokstudiow>5</rokstudiow> </student> 21 Deklaracja globalna elementów i atrybutów umieszczona w elemencie głównym schema zakres widoczności: cały schemat Deklaracja lokalna umieszczona wewnątrz deklaracji elementu / definicji typu brak nazwy typu Referencja odsyłacz do zadeklarowanych elementów i/lub atrybutów 22 11
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/schema"> <!-- globalna deklaracja elementu --> <xsd:element name="nazwisko" type="xsd:string"/> <xsd:element name="osoba"> <!-- lokalna definicja typu --> <xsd:complextype> <xsd:sequence> <!-- odsyłacz do elementu globalnego --> <xsd:element ref="nazwisko"/> <xsd:element name="imie" type="xsd:string"/> </xsd:sequence> </xsd:element> </xsd:schema> 23 minoccurs(domyślnie 1) maxoccurs(domyślnie 1) unbounded(liczba wystąpień nieokreślona) <xsd:element name="imie" type="xsd:string" minoccurs="1" maxoccurs="2"/> <xsd:element name="nrtelefonu" type="xsd:string" minoccurs="0" maxoccurs="unbounded"/> 24 12
wymagany (required) opcjonalny (optional) (wartość domyślna) zakazany (prohibited) <xsd:attribute name="plec" type="xsd:string" use="required"/> <xsd:attribute name="stawkavat" type="xsd:string" use="optional" default="22"/> <xsd:attribute name="opis" type="xsd:string" use="prohibited"/> 25 Określenie sposobu występowania subelementów (elementów potomnych) sequence(wszystkie w podanej kolejności) all(wszystkie w dowolnej kolejności) choice(jeden z wymienionych) 26 13
<!-- sequence --> <xsd:complextype name="typosoba"> <xsd:sequence> <xsd:element name="nazwisko" type="xsd:string"/> <xsd:element name="imie" type="xsd:string"/> </xsd:sequence> <!-- choice --> <xsd:complextype name="typdochod"> <xsd:choice> <!-- pobory pracownika --> <xsd:element name="pobory" type="xsd:decimal"/> <!-- dochód emeryta --> <xsd:element name="emerytura" type="xsd:decimal"/> </xsd:choice> 27 <!-- definicja typu złoŝonego --> <xsd:complextype name="typosoba"> <xsd:sequence> <xsd:element name="nazwisko" type="xsd:string"/> <xsd:element name="imie" type="xsd:string"/> </xsd:sequence> <! - rozszerzenie TypOsoba o dodatkowe dane o studencie --> <xsd:complextype name="typstudent"> <xsd:extension base="typosoba"> <xsd:sequence> <xsd:element name="rokstudiow" type="xsd:integer"/> <xsd:element name="kierunekstudiow" type="xsd:string"/> </xsd:sequence> </xsd:extension> 28 14
<!-- definicja typu prostego (pochodna typu string) --> <xsd:simpletype name="typnazwisko"> <xsd:restriction base="xsd:string"> <xsd:minlength value="2"/> <xsd:maxlength value="40"/> </xsd:restriction> </xsd:simpletype> 29 dopuszczalne wartości elementów i atrybutów length, minlength, maxlength maxinclusive, maxexclusive, mininclusive, minexclusive totaldigits, fractiondigits pattern(regular expressions wyrażenia regularne) enumeration(wyliczenia - słowniki) whitespace preserve(bez normalizacji ciągu - wart. domyślna) replace (zamiana białych znaków na spacje) collapse (zamiana spacji na pojedynczą spację) 30 15
<!-- aspekt enumeration --> <!-- lista dopuszczalnych wartości --> <xsd:simpletype name="typstancywilny"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="panna"/> <xsd:enumeration value="kawaler"/> <xsd:enumeration value="ŝonaty"/> <xsd:enumeration value="męŝatka"/> <xsd:enumeration value="wdowa"/> </xsd:restriction> </xsd:simpletype> 31 <!-- lista 6 liczb z 49 --> <wynikiduzegolotka>5 12 40 6 28 10</wynikiDuzegoLotka> <xsd:simpletype name="liczbaduzylotek"> <xsd:restriction base="xsd:positiveinteger"> <xsd:maxinclusive value="49"/> </xsd:restriction> </xsd:simpletype> <xsd:simpletype name="listaliczb"> <xsd:list itemtype="liczbaduzylotek"/> </xsd:simpletype> <xsd:simpletype name="liczbyduzegolotka"> <xsd:restriction base="listaliczb"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpletype> <xsd:element name="losowanie" type="liczbyduzegolotka"/> 32 16