Writing XML Schemas

Most of this chapter centers on an example document, ch05_06.xml, and its accompanying schema, ch05_07.xsd. (You can use these examples with Internet Explorer's MSXML 4.0; it will validate ch05_06.xml using ch05_07.xsd.) This example is all about recording the books loaned by one person, Doug Glass, and borrowed by another, Britta Regensburg. I record the name and address of the borrower and lender, as well as data about the actual books borrowed, including their titles, publication date, replacement value, and the maximum number of days the book may be loaned for. Here's what ch05_06.xml looks like:

Listing ch05_06.xml
 <?xml version="1.0"?> <transaction borrowDate="2003-10-15">     <Lender phone="607.555.2222">         <name>Doug Glass</name>         <street>416 Disk Drive</street>         <city>Medfield</city>         <state>MA</state>     </Lender>     <Borrower phone="310.555.1111">         <name>Britta Regensburg</name>         <street>219 Union Drive</street>         <city>Medfield</city>         <state>CA</state>     </Borrower>     <note>Lender wants these back in two weeks!</note>     <books>         <book bookID="123-4567-890">             <bookTitle>Earthquakes for Breakfast</bookTitle>             <pubDate>2003-10-20</pubDate>             <replacementValue>15.95</replacementValue>             <maxDaysOut>14</maxDaysOut>         </book>         <book bookID="123-4567-891">             <bookTitle>Avalanches for Lunch</bookTitle>             <pubDate>2003-10-21</pubDate>             <replacementValue>19.99</replacementValue>             <maxDaysOut>14</maxDaysOut>         </book>         <book bookID="123-4567-892">             <bookTitle>Meteor Showers for Dinner</bookTitle>             <pubDate>2003-10-22</pubDate>             <replacementValue>11.95</replacementValue>             <maxDaysOut>14</maxDaysOut>         </book>         <book bookID="123-4567-893">             <bookTitle>Snacking on Volcanoes</bookTitle>             <pubDate>2003-10-23</pubDate>             <replacementValue>17.99</replacementValue>             <maxDaysOut>14</maxDaysOut>         </book>     </books> </transaction> 

Note in particular that this document has a root element named < transaction > and various subelements such as <Lender> , <Borrower> , and <books> . In fact, the subelements themselves have elements, such as the multiple <book> elements inside the <books> element.

In terms of XML schemas, elements that enclose subelements or that have attributes are complex types. Elements that enclose only simple data such as numbers , strings, or datesbut do not have any subelementsare simple types. In addition, attributes are always simple types because attribute values cannot contain any structure. If you look at a document as a tree, simple types have no subnodes, while complex types can have subnodes.

The distinction between simple and complex types is an important one because you declare these types differently. You declare complex types yourself; the XML schema specification comes with many simple types already declared, as we'll see. You can also declare your own simple types, and we'll how to do that as well.

Here's the schema for the document ch05_06.xml; this schema is named ch05_07.xsd, and the prefix xsd: is a prefix used by convention to indicate a W3C schema namespace. Besides xsd , you often find the prefix xs used for schema namespaces these dayswhich namespace you use is a matter of choice, or is up to the application you're working with. Note that like all schemas, ch05_07.xsd is a well- formed XML document:

Listing ch05_07.xsd
 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">     <xsd:annotation>         <xsd:documentation>             Book borrowing transaction schema.         </xsd:documentation>     </xsd:annotation>     <xsd:element name="transaction" type="transactionType"/>     <xsd:complexType name="transactionType">         <xsd:sequence>             <xsd:element name="Lender" type="address"/>             <xsd:element name="Borrower" type="address"/>             <xsd:element ref="note" minOccurs="0"/>             <xsd:element name="books" type="books"/>         </xsd:sequence>         <xsd:attribute name="borrowDate" type="xsd:date"/>     </xsd:complexType>     <xsd:element name="note" type="xsd:string"/>     <xsd:complexType name="address">         <xsd:sequence>             <xsd:element name="name" type="xsd:string"/>             <xsd:element name="street" type="xsd:string"/>             <xsd:element name="city" type="xsd:string"/>             <xsd:element name="state" type="xsd:NMTOKEN"/>         </xsd:sequence>         <xsd:attribute name="phone" type="xsd:string"             use="optional"/>     </xsd:complexType>     <xsd:complexType name="books">         <xsd:sequence>             <xsd:element name="book" minOccurs="0" maxOccurs="10">                 <xsd:complexType>                     <xsd:sequence>                         <xsd:element name="bookTitle" type="xsd:string"/>                         <xsd:element name="pubDate" type="xsd:date" minOccurs='0'/>                         <xsd:element name="replacementValue" type="xsd:decimal"/>                         <xsd:element name="maxDaysOut">                             <xsd:simpleType>                                 <xsd:restriction base="xsd:integer">                                     <xsd:maxExclusive value="14"/>                                 </xsd:restriction>                              </xsd:simpleType>                         </xsd:element>                     </xsd:sequence>                     <xsd:attribute name="bookID" type="catalogID"/>                 </xsd:complexType>             </xsd:element>         </xsd:sequence>     </xsd:complexType>     <xsd:simpleType name="catalogID">         <xsd:restriction base="xsd:string">             <xsd:pattern value="\d{3}-\d{4}-\d{3}"/>         </xsd:restriction>     </xsd:simpleType> </xsd:schema> 

We'll go through the various parts of this schema in this chapter, but you can already see some of the structure here. Note, for example that you use a particular namespace in XML schemas ( "http://www.w3.org/2001/XMLSchema" ) and that the schema elements such as <xsd:element> are part of that namespace (you don't have to use the prefix xsd: , but it's conventional). As you can see, elements are declared with the <xsd:element> element, and attributes are declared with the <xsd:attribute> element. Furthermore, you specify the type of elements and attributes when you declare them. To create types, you can use the <xsd:complexType> and <xsd:simpleType> elements (you can then either create elements from the simple or complex types you've created or use the built-in simple types), schema annotations with the <xsd:annotation> element, and so on.

In this case, the root element of the document, <transaction> , is defined to be of the type transactionType . This element can contain several other elements, including those of the address and books types. The address type itself is defined to contain elements that hold a person's name and address. The books type holds elements named <book> that describe a book, including its title, publication date, and so on. Using this schema, you can describe the syntax of ch05_06.xml completely. I'll start taking this schema apart now as we explore it piece by piece.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net