<< Chapter < Page Chapter >> Page >

Once again, according to the XML FAQ :

Figure 2 . Why do we need well-formed XML documents?
"For example, HTML's<img>element is defined as `EMPTY': it doesn't have an end-tag. Without a DTD, an XML applicationwould have no way to know whether or not to expect an end-tag for an element, so the concept of `well-formed' has beenintroduced. This makes the start and end of every element, and theoccurrence of EMPTY elements completely unambiguous."

All XML documents must be well-formed

XML documents need not be valid, but ALL XML DOCUMENTS MUST BE WELL-FORMED .

To be well-formed...

A well-formed XML document must meet several different criteria.

To begin with, in a well-formed XML document, all elements that can contain character data must have both start and end tags.

What is character data?

For purposes of this explanation, let's just say that the content that we discussed earlier comprises character data.

Attribute values must be in quotes

All attribute values must be in quotes (apostrophes or double quotes) . You can surround the value with apostrophes (single quotes) if the attribute value contains a double quote. An attribute value that is surrounded by double quotescan contain apostrophes.

Dealing with empty elements

EMPTY elements (those that contain no character data) must be written in one of the two ways shown in Listing 3, and for several reasons, the first way is usually considered preferable.

Listing 3 . Required syntax for an empty element.
<mx:Button label="My button."/><mx:Button label="My button."></mx:Button>

Don't forget that even an EMPTY element can contain one or more attributes along with namespace information inside the start tag. (In the case of Listing 3, mx: is namespace information and the label information is an attribute.)

Markup characters and entities

There are also rules regarding the inclusion of markup characters.

No markup characters are allowed

For a document to be well-formed, it must not have markup characters such as angle brackets or ampersands in the textdata. If such characters are needed, you can represent them using&lt; and&amp; instead. These special combinations of characters that represent othercharacters, such as&lt; that represents the left angle bracket are called entities.

Nesting

Elements must nest properly. If one element contains another element, the entire second element must be defined inside the start and end tags of the firstelement. Every element in an XML document, other than the root element, is nested inside another element.

Validity and well-formed requirements recap

Valid XML files are those that have a DTD and that conform to the DTD.

All XML files must be well-formed, but there is no requirement for them to be valid.

A DTD is not required in which case validity is impossible to establish. However, if XML documents do have a DTD, they must conform to it, which makesthem valid.

Why use a DTD if it is not required?

There are many reasons to use a DTD, in spite of the fact that XML doesn't require one. One reason is that the use of a DTD makes it possible to enforceformat specifications. For example, in a document that represents a book, the DTD could require that paragraph elements can occur only inside of pageelements. It could also require that page elements can occur only inside chapter elements. It could require that there be a preface element and that it mustoccur before any chapter elements.

Enforcing format specifications

For example, by creating this document using Amaya and the DTD for XHTML, I was required to produce a document that conformed to the DTD for XHTMLdocuments. Otherwise, I would have gotten warnings from the editor and would have been required to acknowledge that the document didn't conform to the DTD in order tosave it.

On one hand, that sounds like a lot of hassle. On the other hand, by creating a document that conforms to the DTD for XHTML, I can be sure that it will renderproperly in any browser that is guaranteed to properly render XHTML documents.

Miscellaneous

This section contains a variety of miscellaneous materials.

Housekeeping material
  • Module name: XML - Well-Formed and Valid Documents
  • File: FlexXhtml0084.htm
  • Revised: 08/17/15
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDFfile for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware thatsome of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.comshowing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, pleasebe aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to xml. OpenStax CNX. Dec 02, 2014 Download for free at https://legacy.cnx.org/content/col11207/1.18
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to xml' conversation and receive update notifications?

Ask