w3resource

XML Proper nesting of elements

Introduction

In xml, elements can not stand alone in the document. All of the elements need to be contained within a hierarchy of elements that begins with the root element. So in a nutshell, if you have three elements in your xml document, the root element must contain the other two.

The easiest way to understand this concept is to simply get into one valuable habit when marking up an xml document: when you create a start tag for an element, immediately create the end tag. Another element can exist within an element, but each element's start tag must have a corresponding end tag before another element's start tag begins. This is the right example for nesting elements properly in a xml document.

<message>
<header>
<from>[email protected]</from>
<to>[email protected]</to>
<subject>On xml</subject>
</header>
<body>

Nesting of xml elements must be proper, in a well-formed xml document.

 </body>
  </message>

This markup would be wrong if the elements weren't nested properly The following will generate an error:

<message>
<header>
<from>[email protected]</from>
<to>[email protected] 
<subject>On xml</to>
</header>
</subject>
<body>
Nesting of xml elements must be proper, in a well-formed xml 
document
</body>
</message>

The preceding code contains an example of an element with another element as content and an element with character data as content. You could also have an empty element mixed in:

<message>
<header>
<from>[email protected]</from>
<to>[email protected]
<subject>On xml</to>
</header>
</subject>
<body>
</message>

Nesting of xml elements must be proper, in a well-formed xml document.<br/> A validating xml parser will generate an error if it is not following the rules set<br/> by the DTD properly.

Notice that the extra space in the <br> element indifference to HTML browsers, which can have trouble with an empty <br> element without space (<br/>) but can handle it otherwise.

Previous: XML Parameter Entities
Next: XML Reserved Markup Characters



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/xml/proper-nesting-of-elements.php