Slajd 1

Transkrypt

Slajd 1
XML – eXtensible Markup Language 2
Przestrzenie nazw
Przykład
Dwa muzea posiadają dokumenty XML i okreslone 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 panują 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>
Prefix xs możemy pominąć.
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
...
...
<schema>
XML Schema
Struktura dokumentu XML schema
<?xml version="1.0"?>
<xs:schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="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"
XML Schema
Struktura dokumentu XML schema
<?xml version="1.0"?>
<xs:schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="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:
<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:
<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
nastepujące atrybuty:
deklaracja
atrybutu
może
posiadać
default="x" - gdzie x jest wartością domyślną elementu
Przykład 5
<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
<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
<attribute name="lang" type="xs:string" use="required"/>
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.
W przypadku braku tych atrybutów wartość x ma domyślną wartość 1.
XML Schema – deklaracje elementów i atrybutów
Przykład 8
<element name="email">
<element name="head" minOccurs="1" maxOccurs="1">
<element name="to" minOccurs="1">
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
XML Schema – typy danych
Typy wbudowane
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
XML Schema – typy danych
Typy proste (simple data types) – definiowane przez ograniczenie typów
wbudowanych.
Przykład 9
<xs:simpleType name="dayOfMonth">
<xs:restriction base="xs:integer">
<minInclusive value="1"/>
<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>
XML Schema – typy danych
Przykład 10
<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>
XML Schema – typy danych
Deklarowanie elementów w oparciu o typy proste.
Przykład 11
<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.
XML Schema – typy danych
To samo ale inaczej...
Przykład 12
<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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 13
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 14
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 15
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 16
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 17
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 18
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 19
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 20
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o wzorzec.
Przykład 21
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o długość.
Przykład 22
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>
XML Schema – typy danych
Ograniczenie wartości w oparciu o długość.
Przykład 23
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>
XML Schema – typy danych
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>
XML Schema – typy danych
Typy definiowane przez użytkownika
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.
XML Schema – typy danych
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>
XML Schema – typy danych
Przykład 26
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>
XML Schema – typy danych
Przykład 27
Inaczej…
<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>
XML Schema – typy danych
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.
XML Schema – typy danych
<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>
XML Schema – typy danych
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>
XML Schema – typy danych
Rozszerzanie typów dany – rozszerzamy 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.
XML Schema – typy danych
<xs:complexType name="extendedLecturerType">
<xs:extension base="lecturerType">
<xs:element name="email" type="xs:string"
minOccurs="0" maxOccurs="1"/>
</xs:extension>
</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>
XML Schema – typy danych
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.
XML Schema – typy danych
<xs:complexType name="restrictedLecturerType">
<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:complexType>
XML Schema – typy danych
Przykład 31
XML schema dla poczty elektronicznej.
<element name="email" type="emailType">
<complexType name="emailType">
<sequence>
<element name="head" type="headType"/>
<element name="body" type="bodyType"/>
</sequence>
</complexType>
XML Schema – typy danych
<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>
XML Schema – typy danych
<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>