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. Dowiesz się również o różnych metodach tworzenia schematów.
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>
Powyższy 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 jest wiersz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", który informuje parser XML, aby weryfikował dokument na podstawie określonego schematu. Wiersz: xsi:noNamespaceSchemaLocation="shiporder.xsd" określa lokalizację schematu (w tym przypadku znajduje się w tym samym folderze 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 ściśle følować 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 definicją języka schema (Schema language definition), jej standardowym wartością to 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. Podczasz elementami "shiporder" otacza element 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 określającym typy danych schema:
<xs:element name="orderperson" type="xs:string"/>
Następnie muszę zdefiniować dwa elementy jako typy złożone: "shipto" i "item". Zaczynam od definiowania 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ń danego elementu, podczas gdy minOccurs definiuje minimalną liczbę wystąpień. Domyślne wartości dla maxOccurs i minOccurs to 1!
Teraz możemy zdefiniować element "item". Ten element może występować wielokrotnie wewnątrz elementu "shiporder". To osiąga się ustawiając wartość atrybutu maxOccurs elementu "item" na "unbounded", co pozwala na pojawienie się elementu "item" w dowolnej liczbie, jaką autor pragnie. 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:Oświadczenie tego atrybutu musi być umieszczone 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ł Schematu
Poprzednia metoda projektowania jest bardzo prosta, ale trudna do odczytania i utrzymania, gdy dokument jest skomplikowany.
Następna metoda projektowania 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 stworzono plik schematu za pomocą nowej metody:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- 简易元素的定义 --> <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życie 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 to: najpierw nazewnictwo prostych i złożonych elementów, a następnie wskazanie 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 jest pochodzenia z przestrzeni nazw W3C XML Schema. Dlatego poniższy wiersz oznacza, że wartość elementu lub atrybutu musi być wartością typu ciąg liter.
<xs:restriction base="xs:string">
Element <restriction> jest często używany do nałożenia ograniczeń na elementy. Oto kilka fragmentów z powyższego schema:
<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ągiem znaków i musi składać się z sześciu ciągłych cyfr, które muszą być cyframi 0-9.
- Poprzednia strona Zastąpienie elementów XSD
- Następna strona Ciąg znaków XSD