Page

18.2.1- Well-Formed Documents

Created by Brendan Doss.
Last Updated by Jim Minatel.  

PublicCategorized as 18. An Introduction to XML.

Not yet tagged

</element>

 

And the whole line, including the opening and closing <to_be_seen> tags, would not be processed or treated as tags by the receiving application.

Comments

It is always good programming practice to comment your code – it so much easier to read if it is commented in a manner that helps explain, reminds you about, or simply points out salient sections of code. It is surprising how code that seemed perfectly clear when you wrote it can soon become a jumble when you come back to it. While the descriptive XML tags often help you understand your own markup, there are times when the tags alone are not enough.

 

The good news is that comments in XML use exactly the same syntax as those in HTML:

 

<!--I really should add a comment here to remind me about xxxxx -->

 

In order to avoid confusing the receiving application, you should not include either the - or -- character in your comment text.

Processing Instructions

These allow documents to contain instructions for applications using the XML data. They take the form:

 

<?NameOfTargetApplication Instructions for Application?>

 

The target name cannot contain the letters xml in any combination of upper or lower case. Otherwise, you can create your own to work with the processing application (unless there are any predefined by the application at which you are targeting your XML).

 

In our next Try It Out, we will be looking at badly formed XML. We can tell a lot about whether our XML is well-formed by simply loading it into Internet Explorer 5. It has the ability to tell us about all sorts of errors (though it does let some slip). When you are first writing XML, it is very helpful to do this quick check so that you know your XML is well-formed.

Try It Out – Badly formed XML

1.    Open up your books.xml file.

2.    Remove the opening <book> tag

3.    Save the file as bad_book.xml

4.    Load it into Internet Explorer 5

Here is the result:

 

Chapter18_image005

As you can see, the error message is pretty accurate. It more or less explicitly tells you that it was expecting an opening <book> tag. It certainly wouldn't take you long to find out what was wrong.

5.    Put the opening book tag in again and change the line:

<title>Beginning ASP 3.0</title>

 

to

<title>Beginning ASP 3.0<title>

 

removing the closing slash.

6.    Save the file again, and open it up in IE5 (or simply click the Refresh button, if you have it open already). You should get a result like this:

 

Chapter18_image006

Again, we are not given the exact error, but IE was expecting a closing <title> tag, which it did not receive.

7.    Finally, correct the closing <title> tag, and remove the opening quote form the US price attribute. Save the file and refresh your browser. This time you get the exact error:

Chapter18_image007

While it is not the most elegant way to test code, it certainly does help find errors quickly. If you have made more than one error, just correct your mistakes one at a time and watch the error messages change.

<< 18.2.0- A Closer Look at Creating XML DocumentsChapter1818.2.2- Valid Documents >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |