Wprowadzenie do XML schema - WSL

Transkrypt

Wprowadzenie do XML schema - WSL
Wprowadzenie do XML schema
podstawowe pojęcia
Przykładowy dokument:
<?xml version="1.0" encoding=’iso-8859-2’ ?>
<library>
<book id="b0836217462" available="true">
<isbn>0836217462</isbn>
<title lang="pl">Wstęp do imagineskopii</title>
<author id="sop">
<name>śledź otrębus podgrobelski</name>
<born>1922-11-26</born>
</author>
</book>
...
</library>
Schemat XML (xml schema):
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
Deklarowanie liczby wystąpień w przypadku elementów odbywa się za pomocą
nadania odpowiednich wartości atrybutom maxOccurs, minOccurs; w przypadku
atrybutów służy do tego atrybut use (o wartościach: required, optional, fixed,
prohibited).
Elementy-dzieci elementu ‘schema’ są globalne, pozostałe są lokalne. Elementy
globalne mogą być wstawiane w inne miejsca schematu za pomocą konstrukcji
z atrybutem ref. Elementy globalne nie mogą wstawiać innych elementów
globalnych (tj. atrybutu ref); nie mogą deklarować liczby wystąpień (atrybuty
maxOccurs, minOccurs oraz use, przy czym ten ostatni dotyczy atrybutów).
Elementy lokalne nie mogą być wstawiane za pomocą ref. Kwalifikowanie nazw
(atrybuty elementFormDefault, attributeFormDefault oraz form odnosi się
wyłącznie do elementów lokalnych.
<xs:element name="name" type="xs:string">
<xsd:annotation>
<xsd:documentation xml:lang="pl">
Nazwisko i imię autora książki.
</xsd:documentation> </xsd:annotation>
</xs:element>
c TP: xml schema: 10 marca 2008
1
<xs:element name="born" type="xs:date"/>
<xs:element name="isbn" type="xs:string"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType> </xs:element>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType> </xs:element>
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType> </xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="id"/>
<xs:attribute ref="available"/>
</xs:complexType> </xs:element>
</xs:schema>
c TP: xml schema: 10 marca 2008
2
<!-- ....................................... -->
<?xml version=’1.0’?>
<xs:schema xmlns=’http://www.w3.org/2001/XMLSchema"
targetNamespace=’http://url’
xmlns=’http://url’
elementFormDefault=’qualified’
<!-- lub attr form -->
attributeFormDefault=’qualified’ > <!-- ditto -->
...
</xs:schema>
deklaracja schematu w dokumencie (nieobowiązkowa):
<?xml version=’1.0’?>
<element xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceLocation="plik.xsd"
<!-- lub: -->
xsi:schemaLocation="http://url plik.xsd" >
...
typy proste
simple types, (atrybuty mogą mieć tylko proste typy):
– wbudowane (predefiniowane):
• xs:string – napis;
• xs:token – #x9 (tab), #xA (linefeed), and #xD (carriage return) są
zamieniane na #20 (odstęp) oraz kolejne odstępy są zamieniane na jeden;
• xs:language – kody języków zdefiniowane w RFC 1766, np.: en, en-US, pl-PL;
• xs:NMTOKEN – ciąg znaków bez znaków odstępu;
• xs:Name – ciąg znaków bez odstępu i zaczynający się od litery, : lub -;
• xs:NCName – jak xs:Name ale znaki : są zabronione;
• xs:ID – jak xs:NCName, ale wartość musi być unikatowa w obrębie
dokumentu (atrybuty/elementy) xs:IDREF – jak xs:NCName, musi być
w dokumencie atrybut/element typu ID o identycznej wartości;
• xs:decimal, xs:integer, xs:float, xs:boolean;
• xs:date, xs:time;
• listy: xs:NMTOKENS, xs:IDREFS, xs:ENTITIES
– definiowane:
c TP: xml schema: 10 marca 2008
3
• poprzez dodawanie ograniczeń (by restriction). Ograniczenia noszą nazwę
aspektów (facets). Technicznie ograniczenie wartości elementu bazowego (do
którego można się odwołać za pomocą atrybutu base) definiowane jest za
pomocą elementu xs:restriction zawierającego jako elementy podrzędne
elementy określające odpowiednie aspekty.
<xs:simpleType name="myInteger">
<xs:restriction base="xs:integer">
<xs:minInclusive value="-2"/>
<xs:maxExclusive value="5"/>
</xs:restriction></xs:simpleType>
<!-- lub: -->
<xs:simpleType name="myInteger">
<xs:restriction>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxExclusive value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:minInclusive value="-2"/>
</xs:restriction></xs:simpleType>
przykłady aspektów: xs:enumeration, xs:length, xs:pattern
• przez tworzenie list (lists):
<xs:simpleType name="integerList">
<xs:list itemType="xs:integer"/>
</xs:simpleType>
przykładami tego typu danych są predefiniowane typy: IDREFS, ENTITIES,
and NMTOKENS:
<xs:simpleType name="nazwa">
<xs:list itemType="xs:token"/> </xs:simpleType>
• przez tworzenie kombinacji (unions):
<xs:simpleType name=’nazwa’>
<xs:union memberTypes="xs:integer xs:date" />
</xs:simpleType>
wzorzec regułowy
Działa na poziomie lexical space, a nie value space. Przykłady:
c TP: xml schema: 10 marca 2008
4
<xs:simpleType name="myByte">
<xs:restriction base="xs:byte">
<xs:enumeration value="1"/>
<xs:enumeration value="5"/>
<xs:enumeration value="15"/>
</xs:restriction></xs:simpleType>
wartości poprawne: 1, 5, 15, 015, 00005 itp...
<xs:simpleType name="myByte">
<xs:restriction base="xs:byte">
<xs:pattern value="1"/>
<xs:pattern value="5"/>
<xs:pattern value="15"/>
</xs:restriction></xs:simpleType>
wartości poprawne: 1, 5, 15 (tylko).
znaki specjalne: ., +, ?, *, [^-], (a|b), \n, \r, \t, {n,m}.
<xs:simpleType name="myByte">
<xs:restriction base="xs:byte">
<xs:pattern value="1|5|15"/>
</xs:restriction> </xs:simpleType>
<xs:simpleType name="multipleOfTen">
<xs:restriction base="xs:integer">
<xs:pattern value=".*0"/>
</xs:restriction> </xs:simpleType>
<!-- inne wzorce (numer u-boota, adres url z domeny .pl):
U-[1-5][0-9]{0-3}
.*\.pl -->
typy złożone
Typy proste opisują węzły tekstowe (elementy lub atrybuty). Typy złożone
opisują strukturę dokumentu.
Typy złożone mogą mieć prosty lub złożony model zawartości.
Oprócz tego wyróżnia się model o mieszanej zawartości (mixed content) i pustej
zawartości.
modele o prostej zawartości
Typy złożone o prostej zawartości powstają przez dodanie listy atrybutów do
prostego typu.
c TP: xml schema: 10 marca 2008
5
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="string255">
<xs:attribute ref="lang"/> </xs:extension>
</xs:simpleContent>
</xs:complexType> </xs:element>
modele o złożonej zawartości
Modele o złożonej zawartości są tworzone za pomocą definiowania listy
elementów i atrybutów. Typowo definicja jest zawarta wewnątrz elementu
xs:complexType i zawiera jeden z następujących elementów: xs:sequence,
xs:choice, xs:all. Każdy z tych elementów zawiera z kolei elementy składowe
(particles)
Elementy xs:sequence, xs:choice mogą posiadać atrybuty minOccurs oraz
maxOccurs. Elementy te mogą także wystąpić jako elementy składowe (tj. mogą
być zagnieżdżone).
Elementy składowe to xs:element, xs:sequence, xs:choice, xs:any i xs:group.
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element ref="name"/>
<xs:sequence>
<xs:element ref="first-name"/>
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:choice>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType> </xs:element>
Grupy elementów i atrybutów. Definicje te muszą być nazwane i globalne.
<xs:group name="name">
<xs:choice>
<xs:element ref="name"/>
<xs:sequence>
<xs:element ref="first-name"/>
c TP: xml schema: 10 marca 2008
6
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:choice> </xs:group>
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:group ref="name"/>
<xs:element ref="born"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType> </xs:element>
Grupy atrybutów mogą być definiowane w analogiczny sposób:
<xs:attributeGroup name="bookAttributes">
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
</xs:attributeGroup>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attributeGroup ref="bookAttributes"/>
</xs:complexType> </xs:element>
rozszerzanie modelu złożonego (derivation by extension)
Funkcjonalnie podobne do połączeniu grup elementów oraz atrybutów. W opinii
wielu lepiej nie korzystać.
ograniczanie modelu złożonego (derivation by restriction)
Wymaga pełnego zdefiniowania modelu zawartości (za wyjątkiem atrybutów,
które można deklarować jako ’prohibited’), który musi być ograniczeniem
modelu wyjściowego (base type). Każda wartość zgodna z typem ograniczonym
musi być zgodna z typem bazowym. W opinii wielu lepiej nie korzystać.
c TP: xml schema: 10 marca 2008
7
zawartość mieszana (mixed content model)
<xs:element name=’nazwa’>
<xs:complexType mixed=’true’>
<xs:choice minOccurs=’0’ maxOccurs=’unbounded’>
<xs:element ref=’nazwa’/>
<xs:element ref=’nazwa’/>
...
</xs:choice>
<xs:attribute ref=’nazwa’/>
zawartość pusta (empty content model)
<xs:element name=’nazwa’>
<xs:complexType>
<xs:attribute ref=’nazwa’/>
<xs:attribute ref=’nazwa’/>
definiowanie wartości niepowtarzalnych
– za pomocą xs:ID oraz xs:IDREF
– za pomocą kluczy (element xs:key) oraz wartości unikatowych (element
xs:unique). Przykład:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="list">
<xs:complexType> <xs:sequence>
<xs:element maxOccurs="unbounded" ref="book"/>
</xs:sequence> </xs:complexType>
<!-- książki mają unikatowe numery isbn -->
<xs:unique name=’book’>
<xs:selector xpath=’book’/>
<xs:field xpath=’@isbn’/> </xs:unique>
<!-- see wskazuje na isbn wew. book -->
<xs:keyref name=’bbk’ refer=’book’>
<xs:selector xpath=’book’/>
<xs:field xpath=’see’/> </xs:keyref>
</xs:element>
<xs:element name="book">
c TP: xml schema: 10 marca 2008
8
<xs:complexType> <xs:sequence>
<xs:element name="author" type="xs:string" maxOccurs=’unbounded’ />
<xs:element name="title" type="xs:string" />
<xs:element name="see" type="xs:string" />
<xs:element name="description" type="xs:string" />
</xs:sequence>
<xs:attribute name="isbn" type="xs:string"/>
</xs:complexType>
<!-- lista autorów dla książki ma być niepowtarzalna -->
<xs:unique name=’authorbook’>
<xs:selector xpath=’author’/>
<xs:field xpath=’.’/>
</xs:unique>
</xs:element> </xs:schema>
klucze (element key) nie mogą być puste, elementy unique mogą.
dołączanie dokumentów zewnętrznych
<xs:include schemaLocation="rowery-czesci.xsd"/>
<xs:import namespace="http://www.rowery.com.pl/ns/czesci"
schemaLocation="parts.xsd"/>
Oparty o XPointer standard XInclude ma zastąpić encje zewnętrzne.
Najważniejsze różnice: dołączanych fragmentów nie trzeba deklarować oraz
możliwe jest dołączenie fragmentu dokumentu XML.
dokumentacja/oprogramowanie
• XMLLib/xmllint http://xmlsoft.org/
• Xerces, http://www.apache.org/xerces/ (samples/sax/counter.java)
• Trang, http://www.thaiopensource.com/download/
• MSV, https://msv.dev.java.net/
• David C. Fallside: XML Schema Part 0: Primer, W3C Recommendation,
2 May 2001 http://www.w3.org/TR/xmlschema-0/
• Kohsuke Kawaguchi: W3C XML Schema: DOs and DON’Ts
http://www.xml.com/pub/a/2001/06/06/schemasimple.html.
xmllint -schema plik.xsd plik.xml
msv schemat plik.xml
xerces -v -s plik.xml
c TP: xml schema: 10 marca 2008
9

Podobne dokumenty