• No results found

White Space in Elements

In document XML Step by Step, Second Edition (Page 62-65)

White space consists of one or more space, tab, carriage-return, or line feed characters. (These characters are represented, respectively, by the decimal values 32, 9, 13, and 10, or by the equivalent hexadecimal values 20, 09,

Chapter 3 Creating Well-Formed XML Documents 57

3Well-Formed Documents

Other times, you insert white space into an element merely to make the XML source easy to read and understand (often a good idea). For instance, in the following source, the line breaks inserted after the <BOOK>,

</TITLE>, and </AUTHOR> tags, and the space characters before the

<TITLE> and <AUTHOR> tags, make the structure of the elements easier to see and aren’t intended to be part of the BOOK element’s actual charac-ter content:

<BOOK>

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

<AUTHOR>Mark Twain</AUTHOR>

</BOOK>

According to the XML specification, however, the XML processor should not try to guess the purpose of various blocks of white space, but rather it must always preserve all white space characters and pass them on to the ap-plication. (The one exception is that in all text it passes to the application, the processor must convert a carriage-return and line feed character pair, or a carriage-return without a following line feed, to a single line feed character.) XML provides a reserved attribute, named xml:space, that you can include in any element to tell the application how you would like it to handle white space contained in that element. (Attributes are discussed later in this chap-ter.) The xml: indicates that this attribute belongs to the xml namespace.

Because this namespace is predefined, you don’t have to declare it. (See

“Using Namespaces” on page 69.) Keep in mind that this attribute has no effect on the XML processor, which always passes on all white space in elements to the application, and the application can use this information in any way, even ignoring it if appropriate.

The two standard values you can assign to this attribute are default, which signals the application that it should use its default way of handling white space, and preserve, which informs the application that it should preserve all white space. The xml:space attribute specification applies to the element in which it occurs and to any nested elements, unless it is overridden by an xml:space attribute specification in a nested element. For example, the xml:space attribute in the following STANZA element tells the application that it should preserve all white space in the STANZA and nested VERSE elements:

<STANZA xml:space=”preserve”>

<VERSE>For the rare and radiant maiden<VERSE>

<VERSE>whom the angels name Lenore--</VERSE>

<VERSE> Nameless here for evermore.</VERSE>

</STANZA>

58 XML Step by Step

If the application abides by this example xml:space specification, it will pre-serve the leading spaces in the last VERSE element as well as the line breaks before and after each VERSE element.

When you get to Chapters 5 and 7 on creating valid documents, keep in mind that in a valid document the xml:space attribute must be declared just like any other attribute. (This will make sense when you read those chapters.) In a document type definition (DTD), you must declare the attribute as an enumerated type, as shown in the following example attribute-list declaration:

<!ATTLIST STANZA xml:space (default|preserve) ‘preserve’>

Remember that when you use the methods for displaying and working with XML discussed in this book, Internet Explorer provides the application, or at least the front end of the application. So you also need to know what the XML application component of Internet Explorer does with the white space that it receives from Internet Explorer’s XML processor. This will tell you whether the white space will be displayed in the browser, or whether it will be available to the Web pages you write to display XML. The way Internet Explorer handles white space depends on which method you use for dis-playing and working with XML documents:

CSS. If you display an XML document using a cascading style sheet (CSS), as explained in Chapters 8 and 9, Internet Explorer handles white space just as it does in an HTML page (regardless of any xml:space settings included in the document). That is, it replaces sequences of white space characters within an element’s text with a single space character, and it discards leading or trailing white space.

To format the text the way you want it, you can use CSS properties.

Data Binding. If you use data binding to display an XML document, as explained in Chapter 10, Internet Explorer automatically preserves all the white space within an XML element to which an HTML element is bound, regardless of any xml:space settings included in the document. An continued

Chapter 3 Creating Well-Formed XML Documents 59

3Well-Formed Documents

In document XML Step by Step, Second Edition (Page 62-65)