What is XML
XML or Extensible Markup Language is a way to encode documents that can be read by a variety of machines and programs. Over the past fifteen years, hundreds of XML-based languages have come into existence. For example, many programs such as Microsoft Office and iWork use XML and also make decent XML document editors. While XML is fairly straightforward and can incorporate multiple languages via Unicode, it does have a formal structure that must be followed. Here we will outline some of the most common XML mistakes to help you troubleshoot any existing problems you may have with an XML document.
Lacking a Root Element
Every XML document must have a root element in order to function. This element is the parent element of the document, with all other elements being the children to it. The root element does not necessarily have to be called <root> but rather it must function as the “head honcho” of the XML document.
Example:
<root>
<child><subchild></subchild></child>
<child><subchild></subchild></child>
</root>
Lacking an Opening or Closing Tag
Each element in an XML document must have an opening and closing tag. This is also standard for other languages, such as HTML where some of the same tags are used. When an element lacks a closing tag, the document attempts to read the remainder of the document in order to locate the tag and will assume that this is all part of the same element, but since it cannot “close” the element, it errors out. Without an opening tag, the same event may occur or it may simply ignore the element, depending on the situation.
Example:
<p>Element 1 Opens and Closes Properly</p>
<p>Element 2 Does Not Close Properly
<p>Element 3 Does Not Close Properly<p>
Element 4 Does Not Open Properly</p>
Lacking Proper Nesting
While it is common in HTML to see incorrect or improper nesting, in XML, all elements must have proper nesting. For example, in HTML, the code '<p><b><i>Hello World</b></i></p> would work fine. In XML, however, this code will cause errors as the nesting is incorrect.
Example:
<p><b><i>This is correct nesting.</i></b></p>
<p><b><i>This is incorrect nesting.</b></i></p>
Tag Capitalization Errors
Each tag set in XML is case-sensitive, meaning if the opening tag is <Begin>, then the closing tag must be </Begin> and not </begin>.
Example:
<Capital>This is correct.</Capital>
<capital>This is incorrect.</Capital>
Using Symbols Instead of Entity References
Since certain characters have values in one or more situations in XML, entity references are used to avoid any conflicts within the XML document. The characters <, >, &, ', and “ all may pose coding errors when used in a different context. The characters < and & must always be replaced when used in XML with “<” and “&”, respectively. The > character (>), apostrophe (') and quotation marks (") all have uses and can be used depending on the situation.
Example:
<message>if username < 6 then</message> – Incorrect, will cause errors.
<p>Hello Everybody & Anybody!</p> – Incorrect, will cause errors.
<p>Hello Everybody & Anybody!</p> Correct.
<p>Look At This <—–</p> Incorrect, will cause errors.
<p>Look At This <—–</p> Correct.
Many basic XML errors are caused not by the complexity of the code but by simple errors that can easily be corrected. Always review code as you are writing it; it is much easier to scan one line at a time when coding than to go back and attempt to error-proof an entire document.
subscribe to the
WARREN PHILLIPS WEBDESIGN NEWSLETTER:
Keep up to date on the latest web technologies, online marketing strategies, and how they can be leveraged to help your business.
We keep your data private. See our privacy policy.