3. CARGAR CON LA REALIDAD:
3.2 DINAMIZAR LAS POTENCIALIDADES DEL SER
3.2.1. Emociones que estimulan el afecto
As you learned in Chapter 2, an XML document consists of two main parts: the prolog and the document element (which is also known as the root element). In addition, following the document element, a well-formed XML document can include comments, processing instructions, and white space (spaces, tabs, or line breaks). Here’s an example of a well-formed XML document that shows the dif-ferent document parts and the items you can add to each part:
Chapter 3 Creating Well-Formed XML Documents 47
Listing 3-1 shows the complete version of this example document. (You’ll find a copy of this listing on the companion CD under the filename Parts.xml.)
Parts.xml
<?xml version=’1.0'?>
<!-- File Name: Parts.xml -->
<?xml-stylesheet type=”text/css” href=”Inventory01.css”?>
<INVENTORY>
<BOOK>
<TITLE>The Adventures of Huckleberry Finn</TITLE>
<AUTHOR>Mark Twain</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>298</PAGES>
<PRICE>$5.49</PRICE>
</BOOK>
<BOOK>
<TITLE>Leaves of Grass</TITLE>
<AUTHOR>Walt Whitman</AUTHOR>
48 XML Step by Step
<BINDING>hardcover</BINDING>
<PAGES>462</PAGES>
<PRICE>$7.75</PRICE>
</BOOK>
<BOOK>
<TITLE>The Legend of Sleepy Hollow</TITLE>
<AUTHOR>Washington Irving</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>98</PAGES>
<PRICE>$2.95</PRICE>
</BOOK>
<BOOK>
<TITLE>The Marble Faun</TITLE>
<AUTHOR>Nathaniel Hawthorne</AUTHOR>
<BINDING>trade paperback</BINDING>
<PAGES>473</PAGES>
<PRICE>$10.95</PRICE>
</BOOK>
<BOOK>
<TITLE>Moby-Dick</TITLE>
<AUTHOR>Herman Melville</AUTHOR>
<BINDING>hardcover</BINDING>
<PAGES>724</PAGES>
<PRICE>$9.95</PRICE>
</BOOK>
<BOOK>
<TITLE>The Portrait of a Lady</TITLE>
<AUTHOR>Henry James</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>256</PAGES>
<PRICE>$4.95</PRICE>
</BOOK>
Chapter 3 Creating Well-Formed XML Documents 49
3Well-Formed Documents
<TITLE>The Turn of the Screw</TITLE>
<AUTHOR>Henry James</AUTHOR>
<BINDING>trade paperback</BINDING>
<PAGES>384</PAGES>
<PRICE>$3.35</PRICE>
</BOOK>
</INVENTORY>
<!-- Comments, processing instructions, and white space can also appear after the document element. -->
<?MyApp Parm1=”value 1" Parm2=”value 2" ?>
Listing 3-1.
The first line of the example document consists of the XML declaration. Al-though technically the XML declaration is optional, the XML specification rec-ommends including it. In addition to making the document self-identifying as XML and specifying the XML version number (which will become important if later versions are developed), it provides a place for including two optional pieces of information: the encoding declaration and the standalone document declaration. The encoding declaration specifies the encoding scheme used for the characters in the document, and is discussed in the sidebar “Characters, Encod-ing, and Languages” on page 77. The standalone document declaration indicates whether the document contains external markup declarations (explained in Chapter 5) that affect the content of the document. It’s covered in the sidebar
“The standalone Document Declaration” on page 159. If you include an XML declaration, it must appear at the very beginning of the document. (You’re not permitted to include even white space characters before the XML declaration.) The version number in the XML declaration can be delimited with either single or double quotes. In general, quoted strings in XML markup—known as literals—
can use either single or double quotes. Thus, both of the following are legal:
<?xml version=’1.0'?>
<?xml version=”1.0"?>
The example document includes a comment in the prolog and another comment following the document element. You’ll learn more about comments in Chapter 4.
The document also contains two blank lines in the prolog and two more blank lines that follow the document element, each labeled “White space.” White space consists of one or more space, tab, carriage-return, or line feed characters.
50 XML Step by Step
To make an XML document more readable to humans, you can freely add white space between XML markup—such as start-tags, end-tags, comments, and pro-cessing instructions—and also in many places within markup—for instance, the space between xml and version at the beginning of the XML declaration in the example document. The processor simply ignores white space unless it’s within an element (that is, between an element start-tag and a matching end-tag, but not within markup). In that case, the processor passes the white space to the ap-plication as part of the character data of the element that contains the white space. For details on the way white space is handled in elements, see the sidebar
“White Space in Elements” on page 56.
The example document has a processing instruction in the prolog and another that follows the document element. I’ll discuss processing instructions in Chapter 4.
Finally, the example document includes the sine qua non of an XML document:
the document element. Creating the document element and the nested elements that it contains is the focus of this chapter.
note
As you’ll learn in Chapter 5, a valid XML document must either contain a docu-ment type declaration or be processed using a separate XML schema file. A document type declaration is an additional component, not included in the example document in Listing 3-1, which you can place anywhere in the prolog (outside other markup) following the XML declaration. A document type dec-laration contains a document type definition (DTD) that defines the content and structure of a valid XML document.