How do I handle whitespace characters in the XML object model?
Sometimes, the XML object model displays a TEXT node that contains white space characters. When whitespace characters are truncated, some confusion is likely to result. For example, the following XML example:
]>
Smith
John
The following trees are generated:
Processing Instruction:xml
Doctype:person
Element:person
TEXT:
Element:lastname
TEXT:
Element:firstname
TEXT:
Both the first and last names are text nodes that contain only whitespace characters, because the content model of the "person" element is MIXED; it contains #PCDATA keywords. The MIXED content model specifies that there can be text between elements. Therefore, the following are also true:
My last name is Smith and my A-name is
John
The result is similar to the following tree:
Element:person
Text:my Last Name is
Element:lastname
Text:and my The name is
Element:firstname
TEXT:
A sentence cannot be understood without the word "is" and the preceding white-space character, and then the white space before the word "and". Therefore, for the MIXED content model, text combinations, whitespace characters, and elements are related. This is not the case for a MIXED content model.
To make the TEXT node with only whitespace characters disappear, delete the #PCDATA keyword from the person element declaration:
The result is a clear tree below:
Processing Instruction:xml
Doctype:person
Element:person
Element:lastname
Element:firstname
What does an XML declaration do?
The XML declaration must be listed at the top of the XML document:
It specifies the following items:
The document is an XML document. A MIME probe can use it to detect whether a file is a type text/xml when it is missing or has not yet specified a MIME type.
The document conforms to the XML 1.0 specification. This is important in the future when there are other versions of XML.
Document character encoding. The encoding attribute is optional and the default is UTF-8.
Note: The XML declaration must be in the first row of the XML document, so the following XML file:
The following parse error was generated:
Invalid XML declaration.
Line 0000002:
Location 0000007:------^
Note: XML declarations are optional. If you need to specify comments or processing instructions at the top, do not put them in the XML declaration. However, the default encoding will be UTF-8.
How do I print my XML document in a readable format?
When you construct a document from scratch with the DOM to produce an XML file, everything is on one line and there is no space between each other. This is the default behavior.
Constructs the default XSL style sheet in Internet Explorer 5 to display and print XML documents in a readable format. For example, if you already have IE5 installed, try viewing the Nospace.xml file. The following tree should be displayed in the browser:
-
-
Xyz
12.56