• No results found

The standalone Document Declaration

In document XML Step by Step, Second Edition (Page 137-141)

As you learned near the beginning of Chapter 3, you can optionally include a standalone document declaration in the XML declaration at the start of an XML document. The standalone document declaration tells the proces-sor whether the document contains any external markup declarations that affect the document content passed to the application. An external markup declaration is one that is contained in an external DTD subset, in an exter-nal parameter entity, or even in an interexter-nal parameter entity. (An interexter-nal parameter entity is included because a non-validating XML processor isn’t required to read its contents, just as it isn’t required to read an external DTD subset or external parameter entity.) Examples of external markup decla-rations that can affect the document’s content include an entity declaration, or an attribute-list declaration that supplies a default attribute value.

If an XML document has external markup declarations, but none of these declarations affects the document content, you should set standalone to yes, as in this XML declaration:

<?xml version=”1.0" standalone=”yes”?>

(As with the version number—1.0 in this example—you can enclose the standalone value in either double or single quotes. If you also include an encoding declaration in the XML declaration, as explained in the sidebar

“Characters, Encoding, and Languages” on page 77, it must go after the version specification but before the standalone document declaration.) If, however, the document contains external markup declarations that af-fect the document’s content, you should set standalone to no or omit the standalone declaration. (If you omit the standalone declaration, the proces-sor will assume the value no.)

Correctly setting the standalone declaration can help the processor process the XML document appropriately. For example, if you correctly set standalone to yes, the processor will, appropriately, generate a fatal well-formedness error if it encounters a reference to an entity but doesn’t find a declaration for that entity among the internal markup declarations. The standalone setting might also help an application correctly interpret the document content it receives from a non-validating processor.

For more information on the standalone document declaration, including a list of all cases where external markup declarations affect a document’s content (and thereby prohibit setting standalone to yes), see the section “2.9 Standalone Document Declaration” in the XML specification at http://www.w3.org/TR/REC-xml.

160 XML Step by Step

■ For The Marble Faun:

<BOOK InStock=”yes” Reviews=”rev_faun1 rev_faun2">

■ For The Turn of the Screw:

<BOOK InStock=”no” Reviews=”rev_screw”>

6 To reflect the new filename you’re going to assign, change the comment at the beginning of the document from:

<!-- File Name: Inventory Valid.xml -->

to:

<!-- File Name: Inventory Valid Entity.xml -->

7 Use your text editor’s Save As command to save a copy of the modified document under the filename Inventory Valid Entity.xml.

Listing 6-1 shows the complete XML document. (You’ll find a copy of this listing on the companion CD under the filename Inventory Valid Entity.xml.) Inventory Valid Entity.xml

<?xml version="1.0"?>

<!-- File Name: Inventory Valid Entity.xml -->

<!DOCTYPE INVENTORY [

<!-- entities for assigning to the BINDING element: -->

<!ENTITY mass "mass market paperback">

<!ENTITY trade "trade paperback">

<!ENTITY hard "hardcover">

<!-- external entities containing reviews -->

<!-- to be assigned to Review attribute of BOOK elements -->

Chapter 6 Defining and Using Entities 161

6Defining Entities

<!ELEMENT INVENTORY (BOOK)*>

<!ELEMENT BOOK (TITLE, AUTHOR, BINDING, PAGES, PRICE)>

<!ATTLIST BOOK InStock (yes|no) #REQUIRED Reviews ENTITIES #IMPLIED>

<!ELEMENT TITLE (#PCDATA | SUBTITLE)*>

<!ELEMENT SUBTITLE (#PCDATA)>

<!ELEMENT AUTHOR (#PCDATA)>

<!ATTLIST AUTHOR Born CDATA #IMPLIED>

<!ELEMENT BINDING (#PCDATA)>

<!ELEMENT PAGES (#PCDATA)>

<!ELEMENT PRICE (#PCDATA)>

]

>

<INVENTORY>

<BOOK InStock="yes">

<TITLE>The Adventures of Huckleberry Finn</TITLE>

<AUTHOR Born="1835">Mark Twain</AUTHOR>

<BINDING>&mass;</BINDING>

<PAGES>298</PAGES>

<PRICE>$5.49</PRICE>

</BOOK>

<BOOK InStock="no" Reviews="rev_leaves">

<TITLE>Leaves of Grass</TITLE>

<AUTHOR Born="1819">Walt Whitman</AUTHOR>

<BINDING>&hard;</BINDING>

<PAGES>462</PAGES>

<PRICE>$7.75</PRICE>

</BOOK>

<BOOK InStock="yes">

<TITLE>The Legend of Sleepy Hollow</TITLE>

<AUTHOR>Washington Irving</AUTHOR>

<BINDING>&mass;</BINDING>

162 XML Step by Step

<PRICE>$2.95</PRICE>

</BOOK>

<BOOK InStock="yes" Reviews="rev_faun1 rev_faun2">

<TITLE>The Marble Faun</TITLE>

<AUTHOR Born="1804">Nathaniel Hawthorne</AUTHOR>

<BINDING>&trade;</BINDING>

<PAGES>473</PAGES>

<PRICE>$10.95</PRICE>

</BOOK>

<BOOK InStock="no">

<TITLE>Moby-Dick <SUBTITLE>Or, The Whale</SUBTITLE></TITLE>

<AUTHOR Born="1819">Herman Melville</AUTHOR>

<BINDING>&hard;</BINDING>

<PAGES>724</PAGES>

<PRICE>$9.95</PRICE>

</BOOK>

<BOOK InStock="yes">

<TITLE>The Portrait of a Lady</TITLE>

<AUTHOR>Henry James</AUTHOR>

<BINDING>&mass;</BINDING>

<PAGES>256</PAGES>

<PRICE>$4.95</PRICE>

</BOOK>

<BOOK InStock="yes">

<TITLE>The Scarlet Letter</TITLE>

<AUTHOR>Nathaniel Hawthorne</AUTHOR>

<BINDING>&trade;</BINDING>

<PAGES>253</PAGES>

<PRICE>$4.25</PRICE>

</BOOK>

<BOOK InStock="no" Reviews="rev_screw">

<TITLE>The Turn of the Screw</TITLE>

163

Creating Valid XML

In document XML Step by Step, Second Edition (Page 137-141)