Table of Contents
Because there are many types of XML documents, either for transforming or validating data, an XML file that contains data is called an instance document. Any XML document must be well-formed which means that:
all element start-tags and end-tags must be properly nested
there should only be one top-level element in the file.
But there are also other peculiarities we will describe shortly in this chapter.
In the rest of this document, we will be using as input the XML instance files shown in
Example 2.2
and Example 2.3. Their global organization is given in
Example 2.1. They describe a wine cellar containing wine bottles defined
in a separate wine catalog. This application was inspired by the
Livre de cave
example used by Benoît Habert in his book on the Common Lisp Object System (CLOS)
programming [23]. Some of the lines in the examples are
marked with numbered
callouts
(such as
) linked to an explanation at the end of each example.
The structure of our instance document files is the following:
CellarBook.xml
(Example 2.2) describes the cellar in four parts:
wine-catalog
(line 9‑) described in an external
file
Wine-Catalog.xml
owner
(line 11‑) name and address
location
(line 21‑) address of the cellar (if
different from that of the owner)
cellar
(line 27‑) list of wine bottle lots
(using codes from the wine catalog) and, for each, the quantity
currently held in the cellar and the purchase date of the lot
WineCatalog.xml
(Example 2.3) gives the description of each wine product
with a code that will correspond to codes of the cellar.
Example 2.1. Outline of
CellarBook.xml
including
WineCatalog.xml
in a different namespace
1 <cellar-book ... xsi:noNamespaceSchemaLocation="CellarBook.xsd" xmlns:cat="http://www.iro.umontreal.ca/lapalme/wine-catalog"> <xi:include href="WineCatalog.xml" ... />5 <wine-catalog schemaLocation="http://www.iro.umontreal.ca/lapalme/wine-catalog WineCatalog.xsd"> <wine name="Domaine de l'Île Margaux" ... >...</wine> <wine name="Riesling Hugel" ... >...</wine> <wine name="Château Montguéret" ... >...</wine> 10 <wine name="Mumm Cordon Rouge" ... >...</wine> <wine name="Prado Rey Roble" ... >...</wine> </wine-catalog> <owner>...</owner> 15 <location>...</location> <cellar> <wine code="C00043125">...</wine> <wine code="C00312363">...</wine> <wine code="C00871996">...</wine> 20 <wine code="C00929026">...</wine> </cellar> </cellar-book>
|
Inclusion of the file
|
Example 2.2. Excerpt of
CellarBook.xml
, the XML instance document holding the content
of the cellar (Example D.1
shows the whole XML content)
1 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE cellar-book [<!ENTITY guy "Guy Lapalme" >
<!ENTITY eacute "é" > <!ENTITY mtl "Montréal" > 5 <!ENTITY GL "&guy;, &mtl;" >]>
<cellar-book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cat="http://www.iro.umontreal.ca/lapalme/wine-catalog" xsi:noNamespaceSchemaLocation="CellarBook.xsd">
<xi:include href="WineCatalog.xml"
10 xmlns:xi="http://www.w3.org/2001/XInclude"/> <owner>
<name>
<first>Jude</first> <family>Raisin</family> 15 </name> <street>1234 rue des Châteaux</street> <city>St-George</city> <province>ON</province> <postal-code>M7W 7S0</postal-code> 20 </owner> <location>
<street>4587 des Futailles</street> <city>Vallée des crus</city> <province>QC</province> 25 <postal-code>H3C 4J8</postal-code> </location>
<cellar>
<wine code="C00043125"> 30 <purchaseDate>2005-06-20</purchaseDate> <quantity>2</quantity> <comment>
<cat:bold>&GL;</cat:bold>: should reorder soon </comment> 35 </wine> ...
<wine code="C00929026"> <purchaseDate>2003-10-15</purchaseDate> <quantity>1</quantity> 40 <comment>for <cat:bold>big</cat:bold> parties</comment> </wine> </cellar> </cellar-book>
|
A
processing instruction
indicating the XML version used. Although XML version 1.1 exists,
very few processors handle it, so most of the time
|
|
Element |
|
Entities can refer to other entities:
|
|
Starting tag indicating that this is an instance file to be validated with a given schema. |
|
Name of the file containing the schema for this instance file. |
|
Inclusion of another XML file describing the wine catalog. |
|
Description of the owner of the cellar book. |
|
Name of the owner of the cellar book. |
|
Location of the owner of the cellar book. |
|
Description of the cellar. |
|
Information about a given wine, such as quantity and comments; the wine is identified by a code that must match one in the wine catalog. |
|
As the
|
|
Description of another wine of the cellar. |
Example 2.3
is the catalog of available types of wines storing information such as their properties
(color, alcoholic strength), their origin, their price and their year of production. This
information was inspired by data found on the web site of the
Société des Alcools du Québec. Other information
such as the
name
, the
code
and
format
are given as attributes within the start-tag. While the value of an element can be an
arbitrarily complex tree of elements, attribute values can only be single string values.
Strings for attribute values must be delimited by either matching
'
or
"
. These delimiters have the same meaning and this convention is convenient
when embedding a quote of one type within a string value. In case the two types of quotes
are needed within a single string, one can use the predefined entities
'
and
"
(explained in
Section 3.1).
The structure of an XML instance file may seem arbitrary and, in a sense, it is. In order to make sure that its processing is efficient, it is important that the structure of the information be in the right format (i.e. embedded within the correct tags and in the correct order) and that all the mandatory information be present. This verification could be performed by the program using the information but it would more interesting to detect errors or lack of information when the instance file is created. Thus the program needing the data can be sure that the file structure follows the expected format. This validation process, similar to the static type checking for a programming language, is explained in the next chapter but before, we will look at namespaces, another important concept in XML instance documents.
Example 2.3. Excerpt of
WineCatalog.xml
, the XML instance document holding the content
of the wine catalog, it is included in
Example 2.2
(Example D.2
shows the XML content)
1 <wine-catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.iro.umontreal.ca/lapalme/wine-catalog
WineCatalog.xsd" xmlns="http://www.iro.umontreal.ca/lapalme/wine-catalog">
5 <wine name="Domaine de l'Île Margaux" ... >
<properties> <color>red</color> <alcoholic-strength>12.5</alcoholic-strength> <nature>still</nature> 10 </properties> <origin> <country>France</country> <region>Bordeaux</region> <producer> 15 SCEA Domaine de L'Île Margaux (B.P. 5) </producer> </origin> <comment>Ready for drinking now</comment> <food-pairing> 20 Accompanies <emph>Bordelaise ribsteak</emph>,
<bold>pork with prunes</bold> or magret de canard. </food-pairing> <price>22.80</price> <year>2002</year> 25 </wine> ... <wine name="Prado Rey Roble" ... > <properties> <color>red</color> 30 <alcoholic-strength>12.5</alcoholic-strength> <nature>still</nature> </properties> <origin> <country>Spain</country> 35 <region>Old Castille</region> <producer>Real Sitio de Ventosilla SA</producer> </origin> <price>35.25</price> <year>2002</year> 40 </wine> </wine-catalog>
|
Start of the wine catalog description. |
|
Namespace and file name of the Schema for this instance file. |
|
Namespace for the wine catalog, useful to differentiate the wine elements that appear both in the catalog and the cellar book. |
|
Description of a wine. |
|
Some words can be emphasized by surrounding them with
|