Latest web development tutorials

Instancja XML Schema

Ten rozdział pokaże jak napisać schematu XML. Dowiesz się również różne sposoby pisania schematu.


dokumenty XML

Spójrzmy na to, zwane "shiporder.xml" dokument 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>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</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 korzeń elementem "shiporder", który zawiera atrybut musi być nazwany "OrderID" z. "Shiporder" element zawiera trzy różne elementy podrzędne "orderperson", "shipTo" i "element". "Element" elementu pojawia się dwukrotnie, a zawiera on "tytuł", opcjonalny "Note" element, a "ilość" i "cena" elementy.

Powyższe linie xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance", powiedział schemat oparty na parsera XML do zatwierdzenia tego dokumentu. Linia: xsi: noNamespaceSchemaLocation = "shiporder.xsd" schema określa pozycję (w tym przypadku "shiporder.xml" w tym samym folderze).


Tworzenie schematu XML

Teraz musimy utworzyć schemat dla dokumentu XML powyżej.

Możemy zacząć od otwarcia nowego pliku i plik ten nazywa się "shiporder.xsd". Aby utworzyć schemat, po prostu trzeba po prostu śledzić strukturę dokumentu XML, każdy element definicji, co znaleźliśmy. Po pierwsze, możemy zacząć określać standardową deklarację XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
</xs:schema>

W powyższym schemacie, używamy standardowych nazw (xs), Uri miejsca związane z tą nazwą jest definicja języka Schemat (Schema) definicji języka, jego wartość średnia jest http://www.w3.org/ 2001 / XMLSchema.

Następnie musimy zdefiniować "shiporder" element. Element ten ma właściwość, która zawiera inne elementy robocze, więc zidentyfikowane jako typ złożony. Sub-element "shiporder" elementem jest xs: ciąg elementem otoczeniu definiuje kolejność podelementów:

<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>

Potem trzeba "orderperson" element jest zdefiniowany jako prosta typu (ponieważ nie zawierają żadnych atrybutów lub inne elementy). Rodzaj (xs: string) jest poprzedzona przez przepisy namespace prefiks tej przestrzeni nazw i instrukcje ustawionego schematu XML schema typy danych związanych z:

<xs:element name="orderperson" type="xs:string"/>

Następnie muszę dwa elementy zdefiniowane jako złożonego typu: "shipTo" i "pozycji". Zaczynamy od zdefiniowania "shipTo" Element zaczyna:

<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>

Według schematu, możemy określić, ile razy dany element może wystąpić przy użyciu maxOccurs i atrybuty minOccurs. maxOccurs określają maksymalną liczbę elementów pojawiają się i minOccurs określa minimalną liczbę wystąpień elementu. Domyślnie maxOccurs i minOccurs jest jeden!

Teraz możemy zdefiniować "Artykuł" element. Ten element może pojawić się wiele razy wewnątrz "shiporder" elementu. Odbywa się to przez "Artykuł" maxOccurs elementu wartość atrybutu jest ustawiona na "nieograniczona" do osiągnięcia, tak, że "artykuł" element może być twórcą pojawia się tyle razy, ile pożądane. Należy pamiętać, że "pamiętać" element jest opcjonalny. atrybut mamy minOccurs tego elementu jest ustawiona na 0 na:

<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ć "shiporder" atrybut elementu. Ponieważ jest to obowiązkowy atrybut, możemy określić sposób korzystania = "required".

Uwaga: Ta właściwość musi być podany na ostatni:

<xs:attribute name="orderid" type="xs:string" use="required"/>

Poniżej znajduje się lista dokumentów, o nazwie "shiporder.xsd" plik schematu:

<?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

W przedniej części metody projektowania jest bardzo proste, ale jest to trudne do odczytania i utrzymać dokumenty są złożone Shique.

Następnie, w zależności od sposobu, opisanych w pierwszej definicji wszystkie elementy i cechy, a następnie za pomocą atrybutu ref odnieść się do nich.

To nowe podejście jest przeznaczony plik schematu ( "shiporder.xsd"):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- definition of simple elements -->
<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"/>

<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements -->
<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>


Korzystanie z określonego typu (typów nazwanych)

Trzecia metoda projektowania definiuje klasę lub typ, dzięki czemu mamy możliwość definiowania elementów wielokrotnego użytku. Specyficzne podejście: pierwszy element prostych i złożonych nazwy elementów, a następnie wpisz atrybut elementu zwrócić się do nich

Ta trzecia metoda to wykorzystanie pliku schematu Design ( "shiporder.xsd"):

<?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 Ograniczenie pokazujący typ danych pochodzących z przestrzeni nazw typu danych W3C XML Schema. Dlatego poniższy fragment oznacza, że ​​wartość wartości element lub atrybut musi być typu ciąg:

<xs:restriction base="xs:string">

Element Ograniczenie często wykorzystywane do nakładania ograniczeń na czynniki atmosferyczne. Rozważmy następujący fragment 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ągiem znaków i musi być kolejne sześć znaków, a te znaki muszą być 0-9.