XML

Basic Components of an XML Document

The most basic components of an XML document are elements, attributes, and comments. To make it easier to understand how these components work in an XML document, we will look at them using Microsoft XML Notepad. XML Notepad is included in the Microsoft Windows DNA XML Resource Kit, which can be found at Microsoft's Web site (msdn.microsoft.com/vstudio/xml/default.asp).

Elements

Elements are used to mark up the sections of an XML document. An XML element has the following form:

  <ElementName>Content</ElementName>

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:

  <ElementName/>
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.

Nesting elements

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.

by BrainBellupdated
Advertisement: