Creating a Well-Formed XML Document - Well-Formed
(Page 4 of 4 )
The above document is well-formed, and we have accomplished our goals. However, our document is still not validated. In order to achieve validation, we will have to set rules for our document. These rules include in what order the elements should be, whether they are required or optional, and how many times they are allowed to appear in a document. The only way to set these rules is via DTD and XML Schemas, which we will begin covering in the next article.
Before I leave you though, I would like to give you this list, which shows all the rules you must follow to create a well-formed document.
Use an XML declaration.
Choose a root element that will encapsulate all other elements in your document.
If an element has content, you must have an open tag and a close tag.
If an element has no content, it must end with a forward-slash(/). Here is an example: <album genre="Rock and Roll" /> Note that there is no content, only an attribute.
Nest your tags properly. Close all child tags PRIOR to closing a parent tag, and close the root tag last.
If you have an attribute with a value, make sure that the value is enclosed in quotation marks (" ").
Ensure all tags are formed properly. Opening tags are like this <sample> and closing tags are like this </sample>
There are editors that can help ensure your document is well-formed; we will discuss those in a later article.
Final Words
You should now know the rules for a well-formed document. In the next few articles you will learn how to use Document Type Definitions (DTDs) and XML Schema to validate and give rules to your documents.
Thank you for reading my article. If you have any questions or comments, please feel free to leave them.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |