Elements are used to mark up the sections of an XML document. An XML element has the following form:
The content is contained within the XML tags.
Although XML tags usually enclose content, you can also have elements that have no content, called empty elements. In XML, an empty element can be represented as follows:
The <ElementName/> XML notation is sometimes called a singleton. In HTML, the empty tag is represented as <ElementName></ElementName>.
In a patient record XML document, for example, PatientName, PatientAge, PatientIllness, and PatientWeight can all be elements of the XML document, as shown here:
<PatientName>John Smith</PatientName> <PatientAge>108</PatientAge> <PatientWeight>155</PatientWeight>
This PatientName element marks the content John Smith as the patient's name, PatientAge marks the content 108 as the patient's age, and PatientWeight marks the content 155 as the patient's weight. Elements provide information about the content in the document and can be used by computer applications to identify each content section. The application can then manipulate the content sections according to the requirements of the application.
In the case of the patient record document, the content sections could be placed into fields for a new record in a patient database or presented to a user in text boxes in a Web browser. The elements will determine what fields or text boxes each content section belongs in-for example, the content marked by the PatientName element will go into the PatientName field in the database or in the txtPName text box in the Web browser. Using elements, the presentation, storage, and transfer of data can be automated.
Elements can be nested. For example, if you wanted to group all the patient information under a single Patient element, you might want to rewrite the patient record example as follows:
<Patient> <PatientName>John Smith</PatientName> <PatientAge>108</PatientAge> <PatientWeight>155</PatientWeight> </Patient>
When nesting elements, you must not overlap tags. The following construction would not be well formed because the </Patient> end tag appears between the tags of one of its nested elements:
<Patient> <PatientName>John Smith</PatientName> <PatientAge>108</PatientAge> <PatientWeight>155</Patient> </PatientWeight>
Thus XML elements can contain other elements. However, the elements must be strictly nested: each start tag must have a corresponding end tag.