XML data can be in a form of document or in a form of data, which gets exchanged between application and server or between applications, etc...

Elements of XML document

An XML document can have following elements:

  • Declaration,
  • Root,
  • Comments,
  • DocType,
  • Elements which comprises of element name, value, namespace, comment, CDATA, entity references.

Consider an XML file in an example given below:

Example of an XML file

Prolog

XML document can have an optional component called prolog which includes XML declaration and Document type declaration (DTD). These components should appear before root of the document and at very first line of the document as given above.

XML Declaration

This is the version of XML used in the document. The latest version number can be fetched from http://w3.org.

Syntax for XML Declaration

<?xml version="1.0"?>

Document Type Declaration (DTD)

DTD is the rules to be followed to make the XML file valid. It can be standard rules or user defined rules. User defined rules becomes handy when data has to be exchanged between a group of users. DTD can be declared in XML file inside <!DOCTYPE> declaration, or defined in external file and referenced inside <!DOCTYPE>.

Comment

When user have to leave a note in XML document, it can be written inside “<!--  --->” notation as in HTML document. The example shows comments on the data of the document.

Processing Instructions

Processing Instructions (PI’s) are not part of document’s character data but they give instructions to the application, to which the XML data is transferred.

Syntax for Processing Instructions in XML

<?target instructions?>

Where,

  • target – It is the application to which instruction is targeted.
  • instructions – Type of information sent to application.

Example of Processing Instructions in XML

1. <?xml-stylesheet href=”countryAreaStyle.css” type=”text/css” ?>

2. <?Country mapping?>

Element

XML elements are the content between element’s start tag to (including) end tag. The elements include text, attributes, and other elements. In the example above, <earth>, <country> are all elements. “Russia”, “Moscow”, “11.5” … etc... are ‘text’ content.

There can be empty element with no content inside it.

Elements names can have letter, digits, hyphens, underscores and periods. They are case sensitive and must start with letter or underscore. They cannot have spaces.

Syntax for Element in XML

<country></country> or

<country /> -> for element with no value inside it.

Attributes

Attributes provide additional information about the elements. They are not part of data, but may be important for software to manipulate data. Attributes can be used as elements as well.

Syntax for Attributes in XML

<earth>totalArea="100" is attribute of <earth>

CDATA

The CDATA section is used when the source code contains markup symbols as text. When such section is enclosed in CDATA the parser ignores the markup characters. The characters inside CDATA section is directly passed to the application.

Syntax for CDATA in XML

<!CDATA[….text….]]>

<!CDATA[

X < 10;

Population > 22%;

*ptr = &a;

]]>

Content

The XML data can be text content or character data. The two characters that cannot be added in elements are, ‘&’ and ‘<’. ‘&’ cannot be used as it is used to begin entity and character reference and ‘<’ marks the start of tag and cannot be used in elements. Such special characters that cannot be used as text directly are represented as entity reference and character reference

Entity Reference

Entity in XML is something which holds data.  The storage unit may be part of the file, or file itself, or a database record, or any other item which can hold data. Entity has many types which are discussed in later part of tutorial.

 

›› go to examples ››