XML extensible Markup Language część 4
Przestrzenie nazw Dwa muzea posiadają dokumenty XML i określone dla nich DTD (lub XML Schema) zawierające opisy eksponatów. Muzeum 1 (DTD 1): <?xml version="1.0" encoding="iso-8859-1"> <catalog> <painting> <title> Memory of the Garden at Etten </title> <artist>vincent Van Gogh</artist> <date>november, 1888</date> <description> Two women look to the left. A third works in her garden. </description> </painting>... </catalog>
Przestrzenie nazw Muzeum 2 (DTD 2): <?xml version="1.0" encoding="iso-8859-1"> <catalog> <painting> <title>the Swing</title> <artist>pierre-auguste Renoir</artist> <date>1876</date> <description> A young girl on a swing. Two men and a toddler watch. </description> </painting>... </catalog> Oba muzea planują wspólną wystawę swoich eksponatów. Informacja o wystawianych eksponatach ma być dostępna w Internecie.
Przestrzenie nazw Katalog Muzeum 1 i Muzeum 2: <?xml version="1.0" encoding="iso-8859-1"> <catalog> <painting> <title>memory of the Garden at Etten</title> <artist>vincent Van Gogh</artist> <date>november, 1888</date> <description> Two women look to the left. A third works in her garden. </description> </painting> <painting> <title>the Swing</title> <artist>pierre-auguste Renoir</artist> <date>1876</date> <description> A young girl on a swing. Two men and a toddler watch. </description> </painting> </catalog>
Przestrzenie nazw Czy mozna w ten sposób łączyć dokumenty XML? Można, ale......mogą pojawić się problemy podczas walidacji jeżeli elementy w dokumencie XML zawierającym katalog Muzeum 1 są zdefiniowane inaczej niż elementy w dokumencie XML zawierającym katalog Muzeum 2. Walidator napotykając dowolny element w dokumencie musi wiedzieć czy element ten jest zdefiniowany W DTD 1 czy w DTD2. Informację taką zapisujemy wykorzystując tzw. przestrzenie nazw. Deklaracja przestrzeni nazw: xmlns:prefix="położenie"
Przestrzenie nazw <?xml version="1.0" encoding="iso-8859-1"> <m1:catalog xmlns:m1="http://www.muzeum1.pl/dtd/" xmlns:m2="http://www.muzeum1.pl/dtd/"> <m1:painting> <m1:title>memory of the Garden at Etten</m1:title> <m1:artist>vincent Van Gogh</m1:artist> <m1:date>november, 1888</m1:date> <m1:description> Two women look to the left. A third works in her garden. </m1:description> </m1:painting> <m2:painting> <m2:title>the Swing</m2:title> <m2:artist>pierre-auguste Renoir</m2:artist> <m2:date>1876</m2:date> <m2:description> A young girl on a swing. Two men and a toddler watch. </m2:description> </m2:painting> </catalog>
XML Schema (Podobnie jak DTD) XML Schema to opracowany przez W3C standard służący do definiowania struktury dokumentu XML. (W przeciwieństwie do DTD) XML Schema jest aplikacją XML. XML pozwala (dokładniej niż DTD) określić strukturę dokumentów XML. XML schema oferuje większą niż DTD ilość typów (elementów, atrybutów, danych). W XML schema możemy definiować własne typy wykorzystując typy wbudowane. Dokumenty zawierające definicje XML Schema zapisuje się zwykle w plikach z rozszerzeniem xsd.
XML Schema Struktura dokumentu XML schema <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema">...... </xs:schema>
XML Schema Struktura dokumentu XML schema <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.abc.org" xmlns="http://www.abc.org" elementformdefault="qualified">... </xs:schema> Określenie przestrzeni nazw do której należą elementy definiowane w danym schemacie XML: targetnamespace="http://www.abc.org" Każdy element zdefiniowany w tym schemacie XML i użyty w dokumencie XML musi być przypisany do tej przestrzeni nazw: elementformdefault="qualified"
XML Schema Odwołanie do dokumentu XML schema <?xml version="1.0"?> <note xmlns="http://www.abc.org" xmlns:xsi="http://www.a1.com/2012/xmlschema-instance" xsi:schemalocation="http://www.abc.org/note.xsd"> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note> Określenie przestrzeni nazw: xmlns:xsi="http://www.a1.com/2012/xmlschema-instance" Określenie położenia schematu XML związanego z (powyższą) przestrzenią nazw: xsi:schemalocation="http://www.abc.org/note.xsd"
XML Schema deklaracje elementów i atrybutów Elementy proste Każdy element prosty dokumentu XML deklarujemy w następujący sposób: <xs:element name="nazwa" type="typ elementu"> Przykład 1 Deklaracje elementów: <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> Przykładowe użycie: <lastname>refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn>
XML Schema deklaracje elementów i atrybutów Oprócz atrybutu type każda deklaracja elementu prostego może posiadać nastepujące atrybuty: default="x" - gdzie x jest wartością domyślną elementu Przykład 2 <xs:element name="color" type="xs:string" default="red"/> fixed="x" - gdzie x jest wartością elementu której nie można zmienić Przykład 3 <xs:element name="color" type="xs:string" fixed="red"/>
XML Schema deklaracje elementów i atrybutów Atrybuty Atrybuty dokumentu XML deklarujemy w następujący sposób: <xs:attribute name="nazwa" type="typ atrybutu"> Przykład 4 Atrybut: <lastname lang="en">smith</lastname> i jego definicja: <xs:attribute name="lang" type="xs:string"/>
XML Schema deklaracje elementów i atrybutów Oprócz atrybutu type każda deklaracja atrybutu może posiadać nastepujące atrybuty: default="x" - gdzie x jest wartością domyślną elementu Przykład 5 <xs:attribute name="lang" type="xs:string" default="en"/> fixed="x" - gdzie x jest wartością elementu której nie można zmienić Przykład 6 <xs:attribute name="lang" type="xs:string" fixed="en"/>
XML Schema deklaracje elementów i atrybutów Atrybuty elementów są domyślnie opcjonalne. Można to zmienić za pomocą atrybutu use="required" Przykład 7 <xs:attribute name="lang" type="xs:string" use="required"/>
XML Schema deklaracje elementów i atrybutów Oprócz atrybutu type każdy element może posiadać nastepujące atrybuty: minoccurs="x" - określa minimalną liczbę wystapień elementu. maxoccurs="x" - określa maksymalną liczbę wystapień elementu (nieograniczona unbounded) W przypadku braku tych atrybutów wartość x ma domyślną wartość 1. Przykład 8 <xs:element name="email"> <xs:element name="head" minoccurs="1" maxoccurs="1"> <xs:element name="to" maxoccurs="unbounded">
XML Schema proste typy danych Typy numeryczne Przykłady xs:byte - liczba całkowita z przedziału -128 127 xs:double - 03.1415292, 6.022E23, 127E-13 xs:float 3.1415292, -03.1415292, 6.022E23, 127E-13 xs:integer - 3, 3000, 349847329847983264983264987326487326487324678346374 Typ logiczny xs:boolean - true, false, 0, 1
XML Schema proste typy danych Typy znakowe Przykłady xs:string xs:ncname dowolna nazwa XML xs:id nazwa XML niepowtarzalna w całym dokumencie XML xs:idref xs:nmtoken xs:nmtokens xs:language kody języków zdefiniowane w ISO 639 np. en, en-us, en-gb, fr-ca
Typy zwiazane z datą i czasem Przykłady xs:time czas w formacie ±hh:mm:ss.xxx (xxx ułamki sekund). Dodatkowa litera Z oznacza czas Greenwich. 03:32:00-05:00 00:00:00Z 08:30:34.121893632178 xs:date data w formacie YYYY-MM-DD±hh:mm 2001-01-01 2002-12-23+12:00-8750-01-01
Typy proste (simple data types) definiowane przez ograniczenie typów wbudowanych. Przykład 9 <xs:simpletype name="dayofmonth"> <xs:restriction base="xs:integer"> <xs:mininclusive value="1"/> <xs:maxinclusive value="31"/> </xs:restriction> </xs:simpletype> Przykład 10 <xs:simpletype name="namestring"> <xs:restriction base="xs:string"> <xs:maxlength value="50"/> </xs:restriction> </xs:simpletype>
Przykład 11 <xs:simpletype name="locationtype"> <xs:restriction base="xs:string"> <xs:enumeration value="work"/> <xs:enumeration value="home"/> <xs:enumeration value="mobile"/> </xs:restriction> </xs:simpletype>
Deklarowanie elementów w oparciu o typy proste. Przykład 12 <xs:element name="car"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:enumeration value="audi"/> <xs:enumeration value="golf"/> <xs:enumeration value="bmw"/> </xs:restriction> </xs:simpletype> </xs:element> gdzie każdy element enumeration określa dopuszczalną wartości.
Możliwe jest rozdzielenie definicji elementu i jego typu: Przykład 13 <xs:element name="car" type="cartype"/>... <xs:simpletype name="cartype"> <xs:restriction base="xs:string"> <xs:enumeration value="audi"/> <xs:enumeration value="golf"/> <xs:enumeration value="bmw"/> </xs:restriction> </xs:simpletype>
Ograniczenie wartości w oparciu o wzorzec. Przykład 14 Wartością może być jedna mała litera z zakresu a-z. <xs:element name="letter"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 15 Wartością są trzy WIELKIE litery z zakresu a-z. <xs:element name="initials"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-z][a-z][a-z]"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 16 Wartością są trzy małe bądź WIELKIE litery z zakresu a-z. <xs:element name="initials"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-za-z][a-za-z][a-za-z]"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 17 Wartością jest jedna z liter x, y, z. <xs:element name="choice"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[xyz]"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 18 Wartością jest pięć cyfr z przedziału 0-9. <xs:element name="prodid"> <xs:simpletype> <xs:restriction base="xs:integer"> <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 19 Wartością jest zero bądź więcej małych liter z zakresu a-z. <xs:element name="letter"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 20 Wartością jest przynajmniej jedna para liter: mała z zakresu a-z, WIELKA z zakresu A-Z. <xs:element name="letter"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][a-z])+"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 21 Wartością jest male lub female. <xs:element name="gender"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="male female"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o wzorzec. Przykład 22 Wartością jest osiem znaków (małych lub WIELKICH liter lub cyfry 0-9). <xs:element name="password"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-za-z0-9]{8}"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o długość. Przykład 23 Wartością jest osiem dowolnych znaków. <xs:element name="password"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpletype> </xs:element>
Ograniczenie wartości w oparciu o długość. Przykład 24 Wartością jest minimum pięć i maksymalnie osiem znaków. <xs:element name="password"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:minlength value="5"/> <xs:maxlength value="8"/> </xs:restriction> </xs:simpletype> </xs:element>
Typy złożone (complex data types) wykorzystujemy elementy i atrybuty. <xs:complextype name="nazwa typu">... </xs:complextype> Definiując typy złożone możemy wykorzystać następujące elementy: sequence ciąg elementów typów wbudowanych (ważna kolejność) all elementy typów wbudowanych z których wszystkie muszą się pojawić. choice elementy do wyboru.
Przykład 25 Deklaracja typu: <xs:complextype name="lecturertype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:attribute name="title" type="xs:string" use="xs:optional"/> </xs:complextype>
Przykład 26 i wykorzystanie zdefiniowanego typu: <xs:element name="wykladowca"> <xs:complextype> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:attribute name="title" type="xs:string" use="xs:optional"/> </xs:complextype> </xs:wykladowca>
Przykład 27 rozdzielenie definicji elementu i typu: <xs:element name="wykladowca" type="lecturertype"> <xs:complextype name="lecturertype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:attribute name="title" type="xs:string" use="xs:optional"/> </xs:complextype>
Przykład 28 Zdefiniujemy typy danych które mogą pojawić się w dokumencie XML będącym listem. <xs:element name="letter"> <xs:complextype mixed="true"> <xs:sequence> <xs:element name="greeting"/> <xs:element name="body"/> <xs:element name="closing"/> </xs:sequence> </xs:complextype> </xs:element> Typ złożony może posiadać atrybut mixed, który określa czy w elemencie danego typu mogą pojawić sie dane znakowe.
<xs:element name="greeting"> <xs:complextype mixed="true"> <xs:choice> <xs:element name="hello"/> <xs:element name="hi"/> <xs:element name="dear"/> </xs:choice> </xs:complextype> </xs:element> <xs:element name="body"> <xs:complextype mixed="true"> <xs:all> <xs:element name="item"/> <xs:element name="price"/> <xs:element name="arrivaldate"/> </xs:all> </xs:complextype> </xs:element>
Formularz listu wygląda następująco: <letter> <greeting><hello/> Bob!</greeting> <body> Thank you for ordering the <item/> ($<price/>), it should arrive by <arrivaldate/>. </body> </letter>
Rozszerzanie typów dany rozszerzamy zdefniowane wcześniej typy danych przez dodanie nowych elementów lub atrybutów. Przykład 29 <xs:complextype name="lecturertype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:attribute name="title" type="xs:string" use="xs:optional"/> </xs:complextype> Chcemy rozszerzyć typ lecturertype do typu extendedlecturertype przez dodanie informacji o adresie e-mail.
<xs:complextype name="extendedlecturertype"> <xs:complexcontent> <xs:extension base="lecturertype"> <xs:element name="email" type="xs:string" minoccurs="0" maxoccurs="1"/> </xs:extension> </xs:complexcontent> </xs:complextype> Otrzymany w ten sposób następujący typ danych: <xs:complextype name="extendedlecturertype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="email" type="xs:string" minoccurs="0" maxoccurs="1"/> </xs:sequence> <xs:attribute name="title" type="xs:string" use="xs:optional"/> </xs:complextype>
Ograniczanie typów dany ograniczamy typ danych przez dodanie więzów (ograniczeń) na pewne wartości. Przykład 30 <xs:complextype name="lecturertype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:attribute name="title" type="xs:string" use="xs:optional"/> </xs:complextype> Chcemy ograniczyć typ lecturertype do typu restrictedlecturertype.
<xs:complextype name="restrictedlecturertype"> <xs:complexcontent> <xs:restriction base="lecturertype"> <xs:sequence> <xs:element name="firstname" type="xs:string" minoccur="1" maxoccur="2"/> </xs:sequence> <xs:attribute name="title" type="xs:string" use="xs:required"/> </xs:restriction> <xs:complexcontent> </xs:complextype>
Przykład 31 XML schema dla poczty elektronicznej (pominięty prefix xs) <element name="email" type="emailtype"> <complextype name="emailtype"> <sequence> <element name="head" type="headtype"/> <element name="body" type="bodytype"/> </sequence> </complextype>
<complextype name="headtype"> <sequence> <element name="from" type="nameadress"/> <element name="to" type="nameadress" minoccurs="1" maxoccurs="unbounded"/> <element name="cc" type="nameadress" minoccurs="0" maxoccurs="unbounded"/> <element name="subject" type="string"/> </sequence> <complextype> <complextype name="nameadress"> <attribute name="name" type="string" use="optional"/> <attribute name="adress" type="string" use="required"/> </xs:complextype>
<complextype name="bodytype"> <sequence> <element name="text" type="string"/> <element name="attachment" minoccurs="0" maxoccurs="unbounded"/> <complextype> <attribute name="encoding" use="default" value="mime"> <simpletype> <restriction base="string"> <enumeration value="mime"/> <enumeration value="binhex"/> </restriction> </simpletype> </attribute> <attribute name="file" type="string" use="required"/> </complextype> </element> </sequence> </complextype>