XML Nesting
In a prior lesson, we showed an example of an XML document. The file contained a root element and a couple of elements that were inside the root element.When an element appears within another element, it is said that the inner element is "nested". The term nested can be related directly to the word "nest". If an element is nested within another element, then it is surrounded, protected, or encapsulated by the outer element (chirp chirp!).
XML - Why We Nest
Besides being such an easy term to understand, nesting also serves a wonderful purpose of keeping order in an XML document. Much like parentheses in a math problem, elements must be closed in the order that they are opened.This means that an element which is nested inside another element must end itself before the outer element. Below are two example XML documents (A & B). One is properly nested and the other has a small problem.
A) XML Code:
<phonebook> <number> <name> </number> </name> </phonebook>
B) XML Code:
<phonebook> <number> <name> </name> </number> </phonebook>
Did you spot the problem with example A? The name element is nested within the number element, yet the number element is closed before name! Example B is what example A must look like to be a well-formed XML document.
Nesting Rules
To think of nesting in plain English, follow this rule: elements opened first must be closed last. That means that the root element, the first element in an XML document, must also be closed last. Nested elements, ones that occur in the middle of the document, must be closed before those that came before them.Once you get the hang of nesting, you'll have no problem creating XML documents. However, this is a common mistake new XML programmers make, so pay close attention to this lesson!
0 comments:
Post a Comment