Tải bản đầy đủ (.pdf) (70 trang)

o'reilly - xml pocket reference 2nd edition

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (422.35 KB, 70 trang )



































XML Pocket Reference, 2nd Edition

Robert Eckstein & Michel Casabianca
Second Edition April 2001

ISBN: 0596001339

XML, the Extensible Markup Language, is the next-generation markup
language for the Web.
It provides a more structured (and therefore more powerful) medium
than HTML, allowing you to define new document types and
stylesheets as needed.
Although the generic tags of HTML are sufficient for everyday text, XML
gives you a way to add rich, well-defined markup to electronic documents.
The XML Pocket Reference is both a handy introduction to XML
terminology and syntax, and a quick reference to XML instructions,
attributes, entities, and datatypes.
Although XML itself is complex, its basic concepts are simple.
This small book combines a perfect tutorial for learning the basics of XML
with a reference to the XML and XSL specifications.
The new edition introduces information on XSLT (Extensible Stylesheet
Language Transformations) and Xpath.







Contents

1.1 Introduction 1
1.2 XML Terminology 2
1.3 XML Reference 9
1.4 Entity and Character References 15
1.5 Document Type Definitions 16
1.6 The Extensible Stylesheet Language 26
1.7 XSLT Stylesheet Structure 27
1.8 Templates and Patterns 28
1.9 XSLT Elements 33
1.10 XPath 50
1.11 XPointer and XLink 58

XML Pocket Reference, 2
nd
edition

p
age 1

1.1 Introduction
The Extensible Markup Language (XML) is a document-processing standard that is an official
recommendation of the World Wide Web Consortium (W3C), the same group responsible for
overseeing the HTML standard. Many expect XML and its sibling technologies to become the markup
language of choice for dynamically generated content, including nonstatic web pages. Many companies
are already integrating XML support into their products.
XML is actually a simplified form of Standard Generalized Markup Language (SGML), an
international documentation standard that has existed since the 1980s. However, SGML is extremely
complex, especially for the Web. Much of the credit for XML's creation can be attributed to Jon Bosak

of Sun Microsystems, Inc., who started the W3C working group responsible for scaling down SGML to
a form more suitable for the Internet.
Put succinctly, XML is a meta language that allows you to create and format your own document
markups. With HTML, existing markup is static:
<HEAD> and <BODY>, for example, are tightly
integrated into the HTML standard and cannot be changed or extended. XML, on the other hand,
allows you to create your own markup tags and configure each to your liking - for example,
<HeadingA>, <Sidebar>, <Quote>, or <ReallyWildFont>. Each of these elements can be defined
through your own document type definitions and stylesheets and applied to one or more XML
documents. XML schemas provide another way to define elements. Thus, it is important to realize that
there are no "correct" tags for an XML document, except those you define yourself.
While many XML applications currently support Cascading Style Sheets (CSS), a more extensible
stylesheet specification exists, called the Extensible Stylesheet Language (XSL). With XSL, you ensure
that XML documents are formatted the same way no matter which application or platform they appear
on.
XSL consists of two parts: XSLT (transformations) and XSL-FO (formatting objects).
Transformations, as discussed in this book, allow you to work with XSLT and convert XML documents
to other formats such as HTML. Formatting objects are described briefly in Section 1.6.1.
This book offers a quick overview of XML, as well as some sample applications that allow you to get
started in coding. We won't cover everything about XML. Some XML-related specifications are still in
flux as this book goes to print. However, after reading this book, we hope that the components that
make up XML will seem a little less foreign.

XML Pocket Reference, 2
nd
edition

p
age
2


1.2 XML Terminology
Before we move further, we need to standardize some terminology. An XML document consists of one
or more elements. An element is marked with the following form:
<Body>
This is text formatted according to the Body element
</Body>.
This element consists of two tags: an opening tag, which places the name of the element between a
less-than sign (
<) and a greater-than sign (>), and a closing tag, which is identical except for the
forward slash (
/) that appears before the element name. Like HTML, the text between the opening and
closing tags is considered part of the element and is processed according to the element's rules.
Elements can have attributes applied, such as the following:
<Price currency="Euro">25.43</Price>
Here, the attribute is specified inside of the opening tag and is called currency. It is given a value of
Euro, which is placed inside quotation marks. Attributes are often used to further refine or modify the
default meaning of an element.
In addition to the standard elements, XML also supports empty elements. An empty element has no
text between the opening and closing tags. Hence, both tags can (optionally) be combined by placing a
forward slash before the closing marker. For example, these elements are identical:
<Picture ll.gif"></Picture>
<Picture src="blueball.gif"/>
Empty elements are often used to add nontextual content to a document or provide additional
information to the application that parses the XML. Note that while the closing slash may not be used
in single-tag HTML elements, it is mandatory for single-tag XML empty elements.
1.2.1 Unlearning Bad Habits
Whereas HTML browsers often ignore simple errors in documents, XML applications are not nearly as
forgiving. For the HTML reader, there are a few bad habits from which we should dissuade you:
XML is case-sensitive

Element names must be used exactly as they are defined. For example,
<Paragraph> and
<paragraph> are not the same.
A non-empty element must have an opening and a closing tag
Each element that specifies an opening tag must have a closing tag that matches it. If it does
not, and it is not an empty element, the XML parser generates an error. In other words, you
cannot do the following:
<Paragraph>
This is a paragraph.
<Paragraph>
This is another paragraph.
Instead, you must have an opening and a closing tag for each paragraph element:
<Paragraph>This is a paragraph.</Paragraph>
<Paragraph>This is another paragraph.</Paragraph>


XML Pocket Reference, 2
nd
edition

p
age
3

Attribute values must be in quotation marks
You can't specify an attribute value as
<picture mages/blueball.gif/>, an error
that HTML browsers often overlook. An attribute value must always be inside single or double
quotation marks, or else the XML parser will flag it as an error. Here is the correct way to
specify such a tag:

<picture src="/images/blueball.gif"/>


Tags must be nested correctly
It is illegal to do the following:
<Italic><Bold>This is incorrect</Italic></Bold>
The closing tag for the <Bold> element should be inside the closing tag for the <Italic>
element to match the nearest opening tag and preserve the correct element nesting. It is
essential for the application parsing your XML to process the hierarchy of the elements:
<Italic><Bold>This is correct</Bold></Italic>
These syntactic rules are the source of many common errors in XML, especially because some of this
behavior can be ignored by HTML browsers. An XML document adhering to these rules (and a few
others that we'll see later) is said to be well-formed.
1.2.2 An Overview of an XML Document
Generally, two files are needed by an XML-compliant application to use XML content:
The XML document
This file contains the document data, typically tagged with meaningful XML elements, any of
which may contain attributes.
Document Type Definition (DTD)
This file specifies rules for how the XML elements, attributes, and other data are defined and
logically related in the document.
Additionally, another type of file is commonly used to help display XML data: the stylesheet.
The stylesheet dictates how document elements should be formatted when they are displayed. Note
that you can apply different stylesheets to the same document, depending on the environment, thus
changing the document's appearance without affecting any of the underlying data. The separation
between content and formatting is an important distinction in XML.
XML Pocket Reference, 2
nd
edition


p
age 4

1.2.3 A Simple XML Document
Example 1.1 shows a simple XML document.
Example 1.1. sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE OReilly:Books SYSTEM "sample.dtd">
<! Here begins the XML data >
<OReilly:Books xmlns:OReilly=''>
<OReilly:Product>XML Pocket Reference</OReilly:Product>
<OReilly:Price>12.95</OReilly:Price>
</OReilly:Books>
Let's look at this example line by line.
In the first line, the code between the
<?xml and the ?> is called an XML declaration. This declaration
contains special information for the XML processor (the program reading the XML), indicating that
this document conforms to Version 1.0 of the XML standard and uses UTF-8 (Unicode optimized for
ASCII) encoding.
The second line is as follows:
<!DOCTYPE OReilly:Books SYSTEM "sample.dtd">
This line points out the root element of the document, as well as the DTD validating each of the
document elements that appear inside the root element. The root element is the outermost element in
the document that the DTD applies to; it typically denotes the document's starting and ending point.
In this example, the
<OReilly:Books> element serves as the root element of the document. The
SYSTEM keyword denotes that the DTD of the document resides in an external file named sample.dtd.
On a side note, it is possible to simply embed the DTD in the same file as the XML document.
However, this is not recommended for general use because it hampers reuse of DTDs.
Following that line is a comment. Comments always begin with

<! and end with >. You can write
whatever you want inside comments; they are ignored by the XML processor. Be aware that
comments, however, cannot come before the XML declaration and cannot appear inside an element
tag. For example, this is illegal:
<OReilly:Books <! This is the tag for a book >>
Finally, the elements <OReilly:Product>, <OReilly:Price>, and <OReilly:Books> are XML
elements we invented. Like most elements in XML, they hold no special significance except for
whatever document rules we define for them. Note that these elements look slightly different than
those you may have seen previously because we are using namespaces. Each element tag can be
divided into two parts. The portion before the colon (
:) identifies the tag's namespace; the portion
after the colon identifies the name of the tag itself.
Let's discuss some XML terminology. The
<OReilly:Product> and <OReilly:Price> elements would
both consider the
<OReilly:Books> element their parent. In the same manner, elements can be
grandparents and grandchildren of other elements. However, we typically abbreviate multiple levels
by stating that an element is either an ancestor or a descendant of another element.
XML Pocket Reference, 2
nd
edition

p
age
5

1.2.3.1 Namespaces
Namespaces were created to ensure uniqueness among XML elements. They are not mandatory in
XML, but it's often wise to use them.
For example, let's pretend that the

<OReilly:Books> element was simply named <Books>. When you
think about it, it's not out of the question that another publisher would create its own
<Books> element
in its own XML documents. If the two publishers combined their documents, resolving a single
(correct) definition for the
<Books> tag would be impossible. When two XML documents containing
identical elements from different sources are merged, those elements are said to collide. Namespaces
help to avoid element collisions by scoping each tag.
In Example 1.1, we scoped each tag with the
OReilly name-space. Namespaces are declared using the
xmlns:
something
attribute, where
something
defines the prefix of the name-space. The attribute
value is a unique identifier that differentiates this namespace from all other namespaces; the use of a
URI is recommended. In this case, we use the O'Reilly URI as the default
namespace, which should guarantee uniqueness. A namespace declaration can appear as an attribute
of any element, in which case the namespace remains inside that element's opening and closing tags.
Here are some examples:
<OReilly:Books xmlns:OReilly=''>

</OReilly:Books>

<xsl:stylesheet xmlns:xsl=''>

</xsl:stylesheet>
You are allowed to define more than one namespace in the context of an element:
<OReilly:Books xmlns:OReilly=''
xmlns:Songline=''>


</OReilly:Books>
If you do not specify a name after the xmlns prefix, the name-space is dubbed the default namespace
and is applied to all elements inside the defining element that do not use a name-space prefix of their
own. For example:
<Books xmlns=''
xmlns:Songline=''>
<Book>
<Title>XML Pocket Reference</Title>
<ISBN>0-596-00133-9</ISBN>
</Book>
<Songline:CD>18231</Songline:CD>
</Books>
Here, the default namespace (represented by the URI ) is applied to the
elements
<Books>, <Book>, <Title>, and <ISBN>. However, it is not applied to the <Songline:CD>
element, which has its own namespace.
Finally, you can set the default namespace to an empty string. This ensures that there is no default
namespace in use within a specific element:
<header xmlns=''
xmlns:OReilly=''
xmlns:Songline=''>
<entry>Learn XML in a Week</entry>
<price>10.00</price>
</header>
Here, the <entry> and <price> elements have no default namespace.
XML Pocket Reference, 2
nd
edition


p
age 6

1.2.4 A Simple Document Type Definition (DTD)
Example 1.2 creates a simple DTD for our XML document.
Example 1.2. sample.dtd
<?xml version="1.0"?>
<!ELEMENT OReilly:Books (OReilly:Product, OReilly:Price)>
<!ATTLIST OReilly:Books
xmlns:OReilly CDATA "">
<!ELEMENT OReilly:Product (#PCDATA)>
<!ELEMENT OReilly:Price (#PCDATA)>
The purpose of this DTD is to declare each of the elements used in our XML document. All document-
type data is placed inside a construct with the characters
<!
something
>.
Each
<!ELEMENT> construct declares a valid element for our XML document. With the second line,
we've specified that the
<OReilly:Books> element is valid:
<!ELEMENT OReilly:Books
(OReilly:Product, OReilly:Price)>
The parentheses group together the required child elements for the element <OReilly:Books>. In this
case, the
<OReilly:Product> and <OReilly:Price> elements must be included inside our
<OReilly:Books> element tags, and they must appear in the order specified. The elements
<OReilly:Product> and <OReilly:Price> are therefore considered children of <OReilly:Books>.
Likewise, the
<OReilly:Product> and <OReilly:Price> elements are declared in our DTD:

<!ELEMENT OReilly:Product (#PCDATA)>
<!ELEMENT OReilly:Price (#PCDATA)>
Again, parentheses specify required elements. In this case, they both have a single requirement,
represented by
#PCDATA. This is shorthand for parsed character data, which means that any
characters are allowed, as long as they do not include other element tags or contain the characters
< or
&, or the sequence ]]>. These characters are forbidden because they could be interpreted as markup.
(We'll see how to get around this shortly.)
The line
<!ATTLIST OReilly:Books xmlns:OReilly CDATA "http:// www.oreilly.com">
indicates that the
<xmlns:OReilly> attribute of the <OReilly:Books> element defaults to the URI
associated with O'Reilly & Associates if no other value is explicitly specified in the element.
The XML data shown in Example 1.1 adheres to the rules of this DTD: it contains an
<OReilly:Books>
element, which in turn contains an
<OReilly:Product> element followed by an <OReilly:Price>
element inside it (in that order). Therefore, if this DTD is applied to the data with a
<!DOCTYPE>
statement, the document is said to be valid.
1.2.5 A Simple XSL Stylesheet
XSL allows developers to describe transformations using XSL Transformations (XSLT), which can
convert XML documents into XSL Formatting Objects, HTML, or other textual output.
As this book goes to print, the XSL Formatting Objects specification is still changing; therefore, this
book covers only the XSLT portion of XSL. The examples that follow, however, are consistent with the
W3C specification.
XML Pocket Reference, 2
nd
edition


p
age
7

Let's add a simple XSL stylesheet to the example:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="
<xsl:output method="html"/>
<xsl:template match="/">
<font size="+1">
<xsl:apply-templates/>
</font>
</xsl:template>
</xsl:stylesheet>
The first thing you might notice when you look at an XSL stylesheet is that it is formatted in the same
way as a regular XML document. This is not a coincidence. By design, XSL stylesheets are themselves
XML documents, so they must adhere to the same rules as well-formed XML documents.
Breaking down the pieces, you should first note that all XSL elements must be contained in the
appropriate
<xsl:stylesheet> outer element. This tells the XSLT processor that it is describing
stylesheet information, not XML content itself. After the opening
<xsl:stylesheet> tag, we see an
XSLT directive to optimize output for HTML. Following that are the rules that will be applied to our
XML document, given by the
<xsl:template> elements (in this case, there is only one rule).
Each rule can be further broken down into two items: a template pattern and a template action.
Consider the line:
<xsl:template match="/">

This line forms the template pattern of the stylesheet rule. Here, the target pattern is the root element,
as designated by
match="/". The / is shorthand to represent the XML document's root element.
The contents of the
<xsl:template> element:
<font size="+1">
<xsl:apply-templates/>
</font>
specify the template action that should be performed on the target. In this case, we see the empty
element
<xsl:apply- templates/> located inside a <font> element. When the XSLT processor
transforms the target element, every element inside the root element is surrounded by the
<font>
tags, which will likely cause the application formatting the output to increase the font size.
In our initial XML example, the
<OReilly:Product> and <OReilly:Price> elements are both
enclosed inside the
<OReilly:Books> tags. Therefore, the font size will be applied to the contents of
those tags. Example 1.3 displays a more realistic example.
In this example, we target the
<OReilly:Books> element, printing the word Books: before it in a
larger font size. In addition, the
<OReilly:Product> element applies the default font size to each of its
children, and the
<OReilly:Price> tag uses a slightly larger font size to display its children,
overriding the default size of its parent,
<OReilly:Books>. (Of course, neither one has any children
elements; they simply have text between their tags in the XML document.) The text
Price: $ will
precede each of

<OReilly:Price>'s children, and the characters + tax will come after it, formatted
accordingly.

XML Pocket Reference, 2
nd
edition

p
age 8

Example 1.3. sample.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="
xmlns:OReilly="">

<xsl:output method="html">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="OReilly:Books">
<font size="+3">
<xsl:text>Books: </xsl:text>
<br/>
<xsl:apply-templates/>
</font>

</xsl:template>
<xsl:template match="OReilly:Product">
<font size="+0">
<xsl:apply-templates/>
<br/>
</font>
</xsl:template>
<xsl:template match="OReilly:Price">
<font size="+1">
<xsl:text>Price: $</xsl:text>
<xsl:apply-templates/>
<xsl:text> + tax</xsl:text>
<br/>
</font>
</xsl:template>
</xsl:stylesheet>
Here is the result after we pass sample.xsl through an XSLT processor:
<html xmlns:OReilly="">
<body>
<font size="+3">
Books: <br>
<font size="+0">
XML Pocket Reference<br>
</font>
<font size="+1">
Price $12.95 + tax
</font>
</font>
</body>
</html>

And that's it: everything needed for a simple XML document! Running the result through an HTML
browser, you should see something similar to Figure 1.1.
Figure 1.1. Sample XML output


XML Pocket Reference, 2
nd
edition

p
age 9

1.3 XML Reference
Now that you have had a quick taste of working with XML, here is an overview of the more common
rules and constructs of the XML language.
1.3.1 Well-Formed XML
These are the rules for a well-formed XML document:
• All element attribute values must be in quotation marks.
• An element must have both an opening and a closing tag, unless it is an empty element.
• If a tag is a standalone empty element, it must contain a closing slash (
/) before the end of
the tag.
• All opening and closing element tags must nest correctly.
• Isolated markup characters are not allowed in text;
< or & must use entity references. In
addition, the sequence
]]> must be expressed as ]]&gt; when used as regular text. (Entity
references are discussed in further detail later.)
• Well-formed XML documents without a corresponding DTD must have all attributes of type
CDATA by default.

XML Pocket Reference, 2
nd
edition

p
age 10

1.3.2 Special Markup
XML uses the following special markup constructs.
<?xml ?>

<?xml version="
number
"
[encoding="
encoding
"]
[standalone="yes|no"] ?>
Although they are not required to, XML documents typically begin with an XML declaration, which
must start with the characters
<?xml and end with the characters ?>. Attributes include:
version
The
version attribute specifies the correct version of XML required to process the document,
which is currently
1.0. This attribute cannot be omitted.
encoding
The
encoding attribute specifies the character encoding used in the document (e.g., UTF-8 or
iso-8859-1). UTF-8 and UTF-16 are the only encodings that an XML processor is required to

handle. This attribute is optional.
standalone
The optional
standalone attribute specifies whether an external DTD is required to parse the
document. The value must be either
yes or no (the default). If the value is no or the attribute is
not present, a DTD must be declared with an XML
<!DOCTYPE> instruction. If it is yes, no
external DTD is required.
For example:
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>




<? ?>

<?
target attribute1
="
value
"
attribute2
="
value
"
?>
A processing instruction allows developers to place attributes specific to an outside application within
the document. Processing instructions always begin with the characters

<? and end with the characters
?>. For example:
<?works document="hello.doc" data="hello.wks"?>
You can create your own processing instructions if the XML application processing the document is
aware of what the data means and acts accordingly.

XML Pocket Reference, 2
nd
edition

p
age 11

<!DOCTYPE>

<!DOCTYPE
root-element
SYSTEM|PUBLIC
["name"] "URI_of_DTD">
The <!DOCTYPE> instruction allows you to specify a DTD for an XML document. This instruction
currently takes one of two forms:
<!DOCTYPE
root-element
SYSTEM "
URI_of_DTD
">
<!DOCTYPE
root-element
PUBLIC "
name

" "
URI_of_DTD
">
SYSTEM
The
SYSTEM variant specifies the URI location of a DTD for private use in the document. For
example:
<!DOCTYPE Book SYSTEM
"

PUBLIC
The
PUBLIC variant is used in situations in which a DTD has been publicized for widespread
use. In these cases, the DTD is assigned a unique name, which the XML processor may use by
itself to attempt to retrieve the DTD. If this fails, the URI is used:
<!DOCTYPE Book PUBLIC "-//O'Reilly//DTD//EN"
"
Public DTDs follow a specific naming convention. See the XML specification for details on
naming public DTDs.

<! >

<!
comments
>
You can place comments anywhere in an XML document, except within element tags or before the
initial XML processing instructions. Comments in an XML document always start with the characters
<! and end with the characters >. In addition, they may not include double hyphens within the
comment. The contents of the comment are ignored by the XML processor. For example:
<! Sales Figures Start Here >

<Units>2000</Units>
<Cost>49.95</Cost>















XML Pocket Reference, 2
nd
edition

p
age 12

CDATA

<![CDATA[ ]]>
You can define special sections of character data, or CDATA, which the XML processor does not
attempt to interpret as markup. Anything included inside a CDATA section is treated as plain text.
CDATA sections begin with the characters

<![CDATA[ and end with the characters ]]>. For example:
<![CDATA[
I'm now discussing the <element> tag of documents
5 & 6: "Sales" and "Profit and Loss". Luckily,
the XML processor won't apply rules of formatting
to these sentences!
]]>
Note that entity references inside a CDATA section will not be expanded.

XML Pocket Reference, 2
nd
edition

p
age 13

1.3.3 Element and Attribute Rules
An element is either bound by its start and end tags or is an empty element. Elements can contain text,
other elements, or a combination of both. For example:
<para>
Elements can contain text, other elements, or
a combination. For example, a chapter might
contain a title and multiple paragraphs, and
a paragraph might contain text and
<emphasis>emphasis elements</emphasis>.
</para>
An element name must start with a letter or an underscore. It can then have any number of letters,
numbers, hyphens, periods, or underscores in its name. Elements are case-sensitive:
<Para>, <para>,
and

<pArA> are considered three different element types.
Element type names may not start with the string
xml in any variation of upper- or lowercase. Names
beginning with
xml are reserved for special uses by the W3C XML Working Group. Colons (:) are
permitted in element type names only for specifying namespaces; otherwise, colons are forbidden. For
example:
Example Comment
<Italic>
Legal
<_Budget>
Legal
<Punch line>
Illegal: has a space
<205Para>
Illegal: starts with number
<repair@log>
Illegal: contains
@ character
<xmlbob>
Illegal: starts with
xml
Element type names can also include accented Roman characters, letters from other alphabets (e.g.,
Cyrillic, Greek, Hebrew, Arabic, Thai, Hiragana, Katakana, or Devanagari), and ideograms from the
Chinese, Japanese, and Korean languages. Valid element type names can therefore include
<são>,
<peut-être>, <più>, and <niño>, plus a number of others our publishing system isn't equipped to
handle.
If you use a DTD, the content of an element is constrained by its DTD declaration. Better XML
applications inform you which elements and attributes can appear inside a specific element.

Otherwise, you should check the element declaration in the DTD to determine the exact semantics.
Attributes describe additional information about an element. They always consist of a name and a
value, as follows:
<price currency="Euro">
The attribute value is always quoted, using either single or double quotes. Attribute names are subject
to the same restrictions as element type names.
XML Pocket Reference, 2
nd
edition

p
age 14

1.3.4 XML Reserved Attributes
The following are reserved attributes in XML.
xml:lang

xml:lang="
iso_639_identifier
"
The xml:lang attribute can be used on any element. Its value indicates the language of the body of the
element. This is useful in a multilingual context. For example, you might have:
<para xml:lang="en">Hello</para>
<para xml:lang="fr">Bonjour</para>
This format allows you to display one element or the other, depending on the user's language
preference.
The syntax of the
xml:lang value is defined by ISO-639. A two-letter language code is optionally
followed by a hyphen and a two-letter country code. Traditionally, the language is given in lowercase
and the country in uppercase (and for safety, this rule should be followed), but processors are expected

to use the values in a case-insensitive manner.
In addition, ISO-3166 provides extensions for nonstandardized languages or language variants. Valid
xml:lang values include notations such as en, en-US, en-UK, en-cockney, i-navajo, and x-minbari.

xml:space

xml:space="default|preserve"
The xml:space attribute indicates whether any whitespace inside the element is significant and should
not be altered by the XML processor. The attribute can take one of two enumerated values:
preserve
The XML application preserves all whitespace (newlines, spaces, and tabs) present within the
element.
default
The XML processor uses its default processing rules when deciding to preserve or discard the
whitespace inside the element.
You should set
xml:space to preserve only if you want an element to behave like the HTML <pre>
element, such as when it documents source code.
XML Pocket Reference, 2
nd
edition

p
age 1
5

1.4 Entity and Character References
Entity references are used as substitutions for specific characters (or any string substitution) in XML.
A common use for entity references is to denote document symbols that might otherwise be mistaken
for markup by an XML processor. XML predefines five entity references for you, which are

substitutions for basic markup symbols. However, you can define as many entity references as you like
in your own DTD. (See the next section.)
Entity references always begin with an ampersand (
&) and end with a semicolon (;). They cannot
appear inside a CDMS but can be used anywhere else. Predefined entities in XML are shown in the
following table:
Entity Char Notes
&amp;
& Do not use inside processing instructions.
&lt;
< Use inside attribute values quoted with
".
&gt;
>
Use after
]] in normal text and inside processing
instructions.
&quot;
" Use inside attribute values quoted with
".
&apos;
' Use inside attribute values quoted with
'.
In addition, you can provide character references for Unicode characters with a numeric character
reference. A decimal character reference consists of the string
&#, followed by the decimal number
representing the character, and finally, a semicolon (
;). For hexadecimal character references, the
string
&#x is followed first by the hexadecimal number representing the character and then a

semicolon. For example, to represent the copyright character, you could use either of the following
lines:
This document is &#169; 2001 by O'Reilly and Assoc.
This document is &#xA9; 2001 by O'Reilly and Assoc.
The character reference is replaced with the "circled-C" (©) copyright character when the document is
formatted.

XML Pocket Reference, 2
nd
edition

p
age 16

1.5 Document Type Definitions
A DTD specifies how elements inside an XML document should relate to each other. It also provides
grammar rules for the document and each of its elements. A document adhering to the XML
specifications and the rules outlined by its DTD is considered to be valid. (Don't confuse this with a
well-formed document, which adheres only to the XML syntax rules outlined earlier.)
1.5.1 Element Declarations
You must declare each of the elements that appear inside your XML document within your DTD. You
can do so with the
<!ELEMENT> declaration, which uses this format:
<!ELEMENT
elementname rule
>
This declares an XML element and an associated rule called a content model, which relates the
element logically to the XML document. The element name should not include
<> characters. An
element name must start with a letter or an underscore. After that, it can have any number of letters,

numbers, hyphens, periods, or underscores in its name. Element names may not start with the string
xml in any variation of upper- or lowercase. You can use a colon in element names only if you use
namespaces; otherwise, it is forbidden.
1.5.2 ANY and PCDATA
The simplest element declaration states that between the opening and closing tags of the element,
anything can appear:
<!ELEMENT library ANY>
The ANY keyword allows you to include other valid tags and general character data within the element.
However, you may want to specify a situation where you want only general characters to appear. This
type of data is better known as parsed character data, or PCDATA. You can specify that an element
contain only PCDATA with a declaration such as the following:
<!ELEMENT title (#PCDATA)>
Remember, this declaration means that any character data that is not an element can appear between
the element tags. Therefore, it's legal to write the following in your XML document:
<title></title>
<title>XML Pocket Reference</title>
<title>Java Network Programming</title>
However, the following is illegal with the previous PCDATA declaration:
<title>
XML <emphasis>Pocket Reference</emphasis>
</title>
On the other hand, you may want to specify that another element must appear between the two tags
specified. You can do this by placing the name of the element in the parentheses. The following two
rules state that a
<books> element must contain a <title> element, and a <title> element must
contain parsed character data (or null content) but not another element:
<!ELEMENT books (title)>
<!ELEMENT title (#PCDATA)>
XML Pocket Reference, 2
nd

edition

p
age 1
7

1.5.2.1 Multiple sequences
If you wish to dictate that multiple elements must appear in a specific order between the opening and
closing tags of a specific element, you can use a comma (
,) to separate the two instances:
<!ELEMENT books (title, authors)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT authors (#PCDATA)>
In the preceding declaration, the DTD states that within the opening <books> and closing </books>
tags, there must first appear a
<title> element consisting of parsed character data. It must be
immediately followed by an
<authors> element containing parsed character data. The <authors>
element cannot precede the
<title> element.
Here is a valid XML document for the DTD excerpt defined previously:
<books>
<title>XML Pocket Reference, Second Edition</title>
<authors>Robert Eckstein with Michel Casabianca</authors>
</books>
The previous example showed how to specify both elements in a declaration. You can just as easily
specify that one or the other appear (but not both) by using the vertical bar (
|):
<!ELEMENT books (title|authors)>
<!ELEMENT title (#PCDATA)>

<!ELEMENT authors (#PCDATA)>
This declaration states that either a <title> element or an <authors> element can appear inside the
<books> element. Note that it must have one or the other. If you omit both elements or include both
elements, the XML document is not considered valid. You can, however, use a recurrence operator to
allow such an element to appear more than once. Let's talk about that now.
1.5.2.2 Grouping and recurrence
You can nest parentheses inside your declarations to give finer granularity to the syntax you're
specifying. For example, the following DTD states that inside the
<books> element, the XML
document must contain either a
<description> element or a <title> element immediately followed
by an
<author> element. All three elements must consist of parsed character data:
<!ELEMENT books ((title, author)|description)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT description (#PCDATA)>
Now for the fun part: you are allowed to dictate inside an element declaration whether a single element
(or a grouping of elements contained inside parentheses) must appear zero or one times, one or more
times, or zero or more times. The characters used for this appear immediately after the target element
(or element grouping) that they refer to and should be familiar to Unix shell programmers. Occurrence
operators are shown in the following table:
Attribute Description
?
Must appear once or not at all (zero or one times)
+
Must appear at least once (one or more times)
*
May appear any number of times or not at all (zero or more times)
XML Pocket Reference, 2

nd
edition

p
age 18

If you want to provide finer granularity to the <author> element, you can redefine the following in the
DTD:
<!ELEMENT author (authorname+)>
<!ELEMENT authorname (#PCDATA)>
This indicates that the <author> element must have at least one <authorname> element under it. It is
allowed to have more than one as well. You can define more complex relationships with parentheses:
<!ELEMENT reviews (rating, synopsis?, comments+)*>
<!ELEMENT rating ((tutorial|reference)*, overall)>
<!ELEMENT synopsis (#PCDATA)>
<!ELEMENT comments (#PCDATA)>
<!ELEMENT tutorial (#PCDATA)>
<!ELEMENT reference (#PCDATA)>
<!ELEMENT overall (#PCDATA)>
1.5.2.3 Mixed content
Using the rules of grouping and recurrence to their fullest allows you to create very useful elements
that contain mixed content. Elements with mixed content contain child elements that can intermingle
with PCDATA. The most obvious example of this is a paragraph:
<para>
This is a <emphasis>paragraph</emphasis> element. It
contains this <link ref="">link</link>
to the W3C. Their website is <emphasis>very</emphasis>
helpful.
</para>
Mixed content declarations look like this:

<!ELEMENT quote (#PCDATA|name|joke|soundbite)*>
This declaration allows a <quote> element to contain text (#PCDATA), <name> elements, <joke>
elements, and/or
<soundbite> elements in any order. You can't specify things such as:
<!ELEMENT memo (#PCDATA, from, #PCDATA, to, content)>
Once you include #PCDATA in a declaration, any following elements must be separated by "or" bars (|),
and the grouping must be optional and repeatable (
*).
1.5.2.4 Empty elements
You must also declare each of the empty elements that can be used inside a valid XML document. This
can be done with the
EMPTY keyword:
<!ELEMENT
elementname
EMPTY>
For example, the following declaration defines an element in the XML document that can be used as
<statuscode/> or <statuscode></statuscode>:
<!ELEMENT statuscode EMPTY>
XML Pocket Reference, 2
nd
edition

p
age 19

1.5.3 Entities
Inside a DTD, you can declare an entity, which allows you to use an entity reference to substitute a
series of characters for another character in an XML document - similar to macros.
1.5.3.1 General entities
A general entity is an entity that can substitute other characters inside the XML document. The

declaration for a general entity uses the following format:
<!ENTITY
name "replacement_characters"
>
We have already seen five general entity references, one for each of the characters <, >, &, ', and ".
Each of these can be used inside an XML document to prevent the XML processor from interpreting
the characters as markup. (Incidentally, you do not need to declare these in your DTD; they are always
provided for you.)
Earlier, we provided an entity reference for the copyright character. We could declare such an entity in
the DTD with the following:
<!ENTITY copyright "&#xA9;">
Again, we have tied the &copyright; entity to Unicode value 169 (or hexadecimal 0xA9), which is the
"circled-C" (©) copyright character. You can then use the following in your XML document:
<copyright>
&copyright; 2001 by MyCompany, Inc.
</copyright>
There are a couple of restrictions to declaring entities:
• You cannot make circular references in the declarations. For example, the following is
invalid:

<!ENTITY entitya "&entityb; is really neat!">
<!ENTITY entityb "&entitya; is also really neat!">
• You cannot substitute nondocument text in a DTD with a general entity reference. The
general entity reference is resolved only in an XML document, not a DTD document. (If you
wish to have an entity reference resolved in the DTD, you must instead use a parameter
entity reference.)
1.5.3.2 Parameter entities
Parameter entity references appear only in DTDs and are replaced by their entity definitions in the
DTD. All parameter entity references begin with a percent sign, which denotes that they cannot be
used in an XML document - only in the DTD in which they are defined. Here is how to define a

parameter entity:
<!ENTITY %
name "replacement_characters"
>
Here are some examples using parameter entity references:
<!ENTITY % pcdata "(#PCDATA)">
<!ELEMENT authortitle %pcdata;>
XML Pocket Reference, 2
nd
edition

p
age
2
0

As with general entity references, you cannot make circular references in declarations. In addition,
parameter entity references must be declared before they can be used.
1.5.3.3 External entities
XML allows you to declare an external entity with the following syntax:
<!ENTITY quotes SYSTEM
"
This allows you to copy the XML content (located at the specified URI) into the current XML
document using an external entity reference. For example:
<document>
<heading>Current Stock Quotes</heading>
&quotes;
</document>
This example copies the XML content located at the URI
into the document when it's run through the XML processor. As you might guess, this works quite well

when dealing with dynamic data.
1.5.3.4 Unparsed entities
By the same token, you can use an unparsed entity to declare non-XML content in an XML document.
For example, if you want to declare an outside image to be used inside an XML document, you can
specify the following in the DTD:
<!ENTITY image1 SYSTEM
" NDATA GIF89a>
Note that we also specify the NDATA (notation data) keyword, which tells exactly what type of unparsed
entity the XML processor is dealing with. You typically use an unparsed entity reference as the value of
an element's attribute, one defined in the DTD with the type
ENTITY or ENTITIES. Here is how you
should use the unparsed entity declared previously:
<image >
Note that we did not use an ampersand (&) or a semicolon (;). These are only used with parsed
entities.
1.5.3.5 Notations
Finally, notations are used in conjunction with unparsed entities. A notation declaration simply
matches the value of an
NDATA keyword (GIF89a in our example) with more specific information.
Applications are free to use or ignore this information as they see fit:
<!NOTATION GIF89a SYSTEM "-//CompuServe//NOTATION
Graphics Interchange Format 89a//EN">
1.5.4 Attribute Declarations in the DTD
Attributes for various XML elements must be specified in the DTD. You can specify each of the
attributes with the
<!ATTLIST> declaration, which uses the following form:
<!ATTLIST
target_element attr_name attr_type default
>
The <!ATTLIST> declaration consists of the target element name, the name of the attribute, its

datatype, and any default value you want to give it.
XML Pocket Reference, 2
nd
edition

p
age
2
1

Here are some examples of legal <!ATTLIST> declarations:
<!ATTLIST box length CDATA "0">
<!ATTLIST box width CDATA "0">
<!ATTLIST frame visible (true|false) "true">
<!ATTLIST person marital
(single | married | divorced | widowed) #IMPLIED>
In these examples, the first keyword after ATTLIST declares the name of the target element (i.e., <box>,
<frame>, <person>). This is followed by the name of the attribute (i.e., length, width, visible,
marital). This, in turn, is generally followed by the datatype of the attribute and its default value.
1.5.4.1 Attribute modifiers
Let's look at the default value first. You can specify any default value allowed by the specified datatype.
This value must appear as a quoted string. If a default value is not appropriate, you can specify one of
the modifiers listed in the following table in its place:
Modifier Description
#REQUIRED
The attribute value must be specified with the element.
#IMPLIED
The attribute value is unspecified, to be determined by the
application.
#FIXED "

value
"
The attribute value is fixed and cannot be changed by the user.
"
value
"
The default value of the attribute.
With the #IMPLIED keyword, the value can be omitted from the XML document. The XML parser must
notify the application, which can take whatever action it deems appropriate at that point. With the
#FIXED keyword, you must specify the default value immediately afterwards:
<!ATTLIST date year CDATA #FIXED "2001">
1.5.4.2 Datatypes
The following table lists legal datatypes to use in a DTD:
Type
Description
CDATA
Character data
enumerated A series of values from which only one can be chosen
ENTITY
An entity declared in the DTD
ENTITIES
Multiple whitespace-separated entities declared in the DTD
ID
A unique element identifier
IDREF
The value of a unique ID type attribute
IDREFS
Multiple whitespace-separated IDREFs of elements
NMTOKEN
An XML name token

NMTOKENS
Multiple whitespace-separated XML name tokens
NOTATION
A notation declared in the DTD

XML Pocket Reference, 2
nd
edition

p
age
2
2

The CDATA keyword simply declares that any character data can appear, although it must adhere to the
same rules as the
PCDATA tag. Here are some examples of attribute declarations that use CDATA:
<!ATTLIST person name CDATA #REQUIRED>
<!ATTLIST person email CDATA #REQUIRED>
<!ATTLIST person company CDATA #FIXED "O'Reilly">
Here are two examples of enumerated datatypes where no keywords are specified. Instead, the
possible values are simply listed:
<!ATTLIST person marital
(single | married | divorced | widowed) #IMPLIED>
<!ATTLIST person sex (male | female) #REQUIRED>
The ID, IDREF, and IDREFS datatypes allow you to define attributes as IDs and ID references. An ID is
simply an attribute whose value distinguishes the current element from all others in the current XML
document.
IDs are useful for applications to link to various sections of a document that contain an
element with a uniquely tagged

ID. IDREFs are attributes that reference other IDs. Consider the
following XML document:
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE sector SYSTEM sector.dtd>
<sector>
<employee empid="e1013">Jack Russell</employee>
<employee empid="e1014">Samuel Tessen</employee>
<employee empid="e1015" boss="e1013">
Terri White</employee>
<employee empid="e1016" boss="e1014">
Steve McAlister</employee>
</sector>
and its DTD:
<!ELEMENT sector (employee*)>
<!ELEMENT employee (#PCDATA)>
<!ATTLIST employee empid ID #REQUIRED>
<!ATTLIST employee boss IDREF #IMPLIED>
Here, all employees have their own identification numbers (e1013, e1014, etc.), which we define in the
DTD with the
ID keyword using the empid attribute. This attribute then forms an ID for each
<employee> element; no two <employee> elements can have the same ID.
Attributes that only reference other elements use the
IDREF datatype. In this case, the boss attribute is
an
IDREF because it uses only the values of other ID attributes as its values. IDs will come into play
when we discuss XLink and XPointer.
The
IDREFS datatype is used if you want the attribute to refer to more than one ID in its value. The IDs
must be separated by whitespace. For example, adding this to the DTD:
<!ATTLIST employee managers IDREFS #REQUIRED>

allows you to legally use the XML:
<employee empid="e1016" boss="e1014"
managers="e1014 e1013">
Steve McAllister
</employee>
The NMTOKEN and NMTOKENS attributes declare XML name tokens. An XML name token is simply a
legal XML name that consists of letters, digits, underscores, hyphens, and periods. It can contain a
colon if it is part of a namespace. It may not contain whitespace; however, any of the permitted
characters for an XML name can be the first character of an XML name token (e.g.,
.profile is a legal

×