XML Elements

To create an XML document, it must contain elements. Lets assume that I want to create a document with the elements LAND, FOREST, TREE, MEADOW, GRASS. Here is how I would use these elements:

<LAND>
   <FOREST>
      <TREE>Oak</TREE>
      <TREE>Pine</TREE>
      <TREE>Maple</TREE>
   </FOREST>
   <MEADOW>
      <GRASS>Bluegrass</GRASS>
      <GRASS>Fescue</GRASS>
      <GRASS>Rye</GRASS>
   </MEADOW>
</LAND>

Each element is enclosed in <> brackets. The ending element has the '/' character before its name. As you can see, there is one element that contains all others, <LAND>. XML requires one element that contains all others. This single element, which in this case is "LAND", is called the root element. The FOREST element contains several TREE elements, and the MEADOW likewise contains several elements of GRASS. Each element that is contained in another ends in that same element and therefore each element is properly nested.

Elements that are included in another element are considered nested. The TREE elements in the above example are nested in the FOREST element. The FOREST element is the parent element to the TREE element and the TREE element is also called the sub-element to the FOREST element. These relationships hold true as you move up and down the element hierarchy. The FOREST and MEADOW elements are sub-elements to the LAND root element.

The below example is not well formed:

<LAND>
   <FOREST>
      <TREE>Oak</TREE>
      <TREE>Pine</TREE>
      <TREE>Maple
   </FOREST>
      </TREE>
   <MEADOW>
      <GRASS>Bluegrass</GRASS>
      <GRASS>Fescue</GRASS>
      <GRASS>Rye</GRASS>
   </MEADOW>
</LAND>


Element Tags

XML elements require both a beginning tag and an ending tag for all elements that have content. Elements with content may be written as:

<TREE>Very large Oak tree</TREE>

Elements with no content may be expressed as:

<NOTHING></NOTHING>

In shorthand it may be expressed as:

<NOTHING/>

Elements with no content may be used to display graphics and other material in the document.

Element Name Requirements

  • Begins with a letter or underscore.
  • The first character may be followed by any combination of letters, numbers, or other ASCII characters.
  • Elements beginning with "XML" whether capatilized or not are reserved.

Element Content

Elements may contain:

  • Nested elements - Other sub-elements.
  • Processing instructions
  • Characters - Normal text.
  • CDATA sections - Used to enter text that contains special characters not displayed normally by the browser such as less than or greater than sign. These signs are used to enclose tags and are special characters. An example CDATA section:
    <![CDATA[
    The < and > characters are displayed normally here.
    ]]>
    
  • Entity references - An entitity reference is precluded by a & sign. It is used to refer to a previously defined entity, much like a variable may be used in a program.
  • Character references - References to characters that are not displayed normally in XMS such as the < or > characters. These characters are represented as &#60 and &#62 respectively and will be presented on the browser as the less than or greater than character they represent.
  • Comments - Comments are included as shown below and may be placed anywhere except inside an element tag (markup).

    <!-- This is a comment and is not displayed by the browser or renderer -->