XML: A Managers Guide (2nd Edition) (Addison-Wesley Information Technology Series)

Creating an "Order" in XML

The earlier examples in this chapter have used somewhat simplified XML documents. To get a proper feel for XML, it helps to see a realistic and complete document. Example 2-6 is a complete order for PC hardware in XML.

You can see immediately that it has some features not seen in previous examples. First, it starts with a prolog . The prolog is header information that appears before the document element. The first line of the prolog must be the XML declaration that identifies the document as an XML document and specifies the XML version. This particular prolog also contains a processing instruction . A processing instruction gives to software special information that may process the document. It starts with "<?" and ends with "?>". It has a target ”in this case, any software that wants to apply a stylesheet to the document ”and one or more attribute-value pairs ”in this case, a reference to the stylesheet file and a MIME type for the file. A document may contain as many processing instructions as necessary, including different instructions for different applications that may process it.

At the end of the prolog, you may optionally declare the type of the document. A document type declaration indicates the DTD to which the document complies and where to find it. It starts with "<!DOCTYPE" and ends with ">". It has the document element, which must match an element defined in the DTD. It also has a place to look for the DTD ”in this case, the local filesystem ”and a handle ”in this case a filename. The next section discusses how DTDs specify the rules that a document's elements must follow.

The second new feature of Example 2-6 is that the body of the document starts with a comment, always a good idea with any software code. Comments can occur anywhere after the prolog. They start with "<! ”" and end with " ”>".

Example 2-6

<?xml version="1.0"?> <?xml-stylesheet href="order.xsl" type="text/xsl" ?> <!DOCTYPE Order SYSTEM "Order.dtd"> <!-- Example Order Form from _XML: A Manager's Guide_ --> <Order source="web" customerType="consumer" currency="USD"> <Addresses> <Address addressType="billship"> <FirstName>Kevin</FirstName> <LastName>Dick</LastName> <Street lineOrder="1">123 Anywhere Lane</Street> <Street lineOrder="2">Apt 1B</Street> <City>Palo Alto</City> <State>CA</State> <Postal>94303</Postal> <Country>USA</Country> </Address> </Addresses> <LineItems> <LineItem ID="line1"> <Product category="Mboard"> 815i Motherboard </Product> <Quantity>1</Quantity> <UnitPrice>200</UnitPrice> </LineItem> <LineItem ID="line2"> <Product category="RAM"> 128 MB PC-133 DIMM </Product> <Quantity>2</Quantity> <UnitPrice>175</UnitPrice> </LineItem> <LineItem ID="line3"> <Product category="CDROM"> 40x CD-ROM </Product> <Quantity>1</Quantity> <UnitPrice>50</UnitPrice> </LineItem> </LineItems> <Payment> <Card cardType="VISA"> <CardHolder>Kevin S Dick</CardHolder> <Number>41280000000000</Number> <Expiration>01/2010</Expiration> </Card> </Payment> </Order>

After the comment comes the document element. In this case, the document element is "Order." It has attributes that indicate where the order came from, the type of customer, and the currency for the quoted prices. The document element has three subelements corresponding to the three major sections of an order: addresses, line items, and payment information.

Most of the rest of the document is self-explanatory. However, there are a few interesting features. First, the "Address" element has an "addressType" attribute, indicating that the contained address should be used as both the billing and shipping address. As you will see in the next section, the DTD will specify that this attribute must have one of the following values: "bill," "ship," and "billship." Using this approach of an attribute with an enumerated set of possible values is a common technique in designing XML documents. Another common technique is to use attributes to indicate the logical order of multiple elements in a list, such as the "lineOrder" attribute of the "Street" element.

Another feature of note is the "ID" attribute of the "LineItem" element. As you will see in the next section, the DTD will specify that this attribute be of a special type ”ID. ID attributes must be unique within the document, allowing software programs to use the attribute as an index when managing a group of the same elements.

Категории