Przykład instancji XSD
- Poprzednia strona Zastąpienie elementów XSD
- Następna strona Ciąg znaków XSD
W tym rozdziale pokażemy, jak napisać XML Schema. Nauczysz się również różnych metod tworzenia schema.
Dokument XML
Zobaczmy dokument XML o nazwie "shiporder.xml":
<?xml version="1.0" encoding="ISO-8859-1"?> <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>George Bush</orderperson> <shipto> <name>John Adams</name> <address>Oxford Street</address> <city>London</city> <country>UK</country> </shipto> <item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder>
Dokument XML zawiera główny element "shiporder", który zawiera atrybut o nazwie "orderid". Element "shiporder" zawiera trzy różne podelementy: "orderperson", "shipto" oraz "item". Element "item" występuje dwa razy, zawiera "title", opcjonalny element "note", "quantity" oraz element "price".
To wiersz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" informuje parser XML, aby weryfikował dokument na podstawie określonego schema. Wiersz: xsi:noNamespaceSchemaLocation="shiporder.xsd" określa lokalizację schema (w tym przypadku znajduje się w tym samym katalogu, co "shiporder.xml").
Utwórz XML Schema
Teraz musimy utworzyć schema dla tego dokumentu XML.
Możemy zacząć od otwarcia nowego pliku i nazwania go "shiporder.xsd". Aby utworzyć schema, musimy po prostu śledzić strukturę dokumentu XML, definiując każdy znaleziony element. Najpierw zaczynamy od standardowego deklarowania XML:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ... ... </xs:schema>
W powyższym schema użyliśmy standardowej przestrzeni nazw (xs), która jest związana z URI języka definicji Schema (Schema language definition), jego standardową wartością jest http://www.w3.org/2001/XMLSchema.
Następnie musimy zdefiniować element "shiporder". Ten element ma atrybut, który zawiera inne elementy, więc uznajemy go za typ złożony. Podelementy elementu "shiporder" są otoczone elementem xs:sequence, który definiuje kolejność podelementów:
<xs:element name="shiporder"> <xs:complexType> <xs:sequence> ... ... </xs:sequence> ... </xs:complexType> </xs:element>
Następnie musimy zdefiniować element "orderperson" jako typ prosty (ponieważ nie zawiera żadnych atrybutów ani innych elementów). Przedrostek typu (xs:string) jest określony przez prefiks przestrzeni nazw, która jest związana z XML schema, które wskazuje na predefiniowane typy danych:
<xs:element name="orderperson" type="xs:string"/>
Następnie muszę zdefiniować dwa elementy jako typy złożone: "shipto" i "item". Zaczynam od definicji elementu "shipto":
<xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
Poprzez schema, możemy użyć atrybutów maxOccurs i minOccurs, aby zdefiniować liczbę możliwych wystąpień danego elementu. maxOccurs definiuje maksymalną liczbę wystąpień elementu, a minOccurs minimalną liczbę wystąpień. Domyślne wartości dla maxOccurs i minOccurs to 1!
Teraz możemy zdefiniować element "item". Element ten może występować wiele razy wewnątrz elementu "shiporder". To osiąga się ustawieniem atrybutu maxOccurs elementu "item" na "unbounded", co pozwala na występowanie elementu "item" dowolną liczbę razy. Proszę zauważyć, że element "note" jest elementem opcjonalnym. Ustawiliśmy już atrybut minOccurs tego elementu na 0:
<xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element>
Teraz możemy zadeklarować atrybuty elementu "shiporder". Ponieważ jest to atrybut obowiązkowy, określamy use="required".
Komentarz:Deklaracja tego atrybutu musi być umieszczona na końcu:
<xs:attribute name="orderid" type="xs:string" use="required"/>
Oto lista dokumentów tego pliku schematu o nazwie "shiporder.xsd":
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson" type="xs:string"/> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema>
Podział Schema
Poprzednia metoda projektowania jest bardzo łatwa, ale trudna do odczytania i utrzymania, gdy dokument jest bardzo złożony.
Następna zaprojektowana metoda opiera się na najpierw zdefiniowaniu wszystkich elementów i atrybutów, a następnie użyciu atrybutu ref do odwoływania się do nich.
To jest plik schematu projektu zaprojektowany nową metodą:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- Definicja elementu prostego --> <xs:element name="orderperson" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> <!-- Definicja atrybutu --> <xs:attribute name="orderid" type="xs:string"/> <!-- Definicja elementu złożonego --> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="address"/> <xs:element ref="city"/> <xs:element ref="country"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element ref="title"/> <xs:element ref="note" minOccurs="0"/> <xs:element ref="quantity"/> <xs:element ref="price"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element ref="orderperson"/> <xs:element ref="shipto"/> <xs:element ref="item" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="orderid" use="required"/> </xs:complexType> </xs:element> </xs:schema>
Używanie określonych typów (Nazwanych Typów)
Trzecia metoda projektowania definiuje klasy lub typy, co daje nam możliwość powtarzania definicji elementów. Sposób działania: najpierw nazwijmy proste i złożone elementy, a następnie wskażmy na nie za pomocą atrybutu type.
To jest plik schematu projektu ("shiporder.xsd") zaprojektowany za pomocą trzeciej metody:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/> </xs:simpleType> <xs:simpleType name="inttype"> <xs:restriction base="xs:positiveInteger"/> </xs:simpleType> <xs:simpleType name="dectype"> <xs:restriction base="xs:decimal"/> </xs:simpleType> <xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType> <xs:complexType name="shiptotype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="address" type="stringtype"/> <xs:element name="city" type="stringtype"/> <xs:element name="country" type="stringtype"/> </xs:sequence> </xs:complexType> <xs:complexType name="itemtype"> <xs:sequence> <xs:element name="title" type="stringtype"/> <xs:element name="note" type="stringtype" minOccurs="0"/> <xs:element name="quantity" type="inttype"/> <xs:element name="price" type="dectype"/> </xs:sequence> </xs:complexType> <xs:complexType name="shipordertype"> <xs:sequence> <xs:element name="orderperson" type="stringtype"/> <xs:element name="shipto" type="shiptotype"/> <xs:element name="item" maxOccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/> </xs:complexType> <xs:element name="shiporder" type="shipordertype"/> </xs:schema>
Element "restriction" wskazuje, że typ danych pochodzi z przestrzeni nazw W3C XML Schema. W związku z tym, poniższy fragment oznacza, że wartość elementu lub atrybutu musi być typu ciąg znaków:
<xs:restriction base="xs:string">
Element "restriction" jest często używany do nałożenia ograniczeń na elementy. Zobacz poniższe fragmenty z powyższego schematu:
<xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType>
Ten kod wskazuje, że wartość elementu lub atrybutu musi być ciągłem znaków i musi składać się z sześciu ciągłych znaków, które muszą być cyframi 0-9.
- Poprzednia strona Zastąpienie elementów XSD
- Następna strona Ciąg znaków XSD