This article explains about XML syntax. You will learn about elements, tags and attributes which are the fundamental parts in a XML document.
The syntax of XML can be thought of at two distinct levels. First, there is the general low-level syntax that specifies rules to be followed by all XML documents. Second, there is the high-level syntax that is distinct for each XML document which can be specified by using either DTD (Document Type Definition) or XML Schema. They specify rules about which tags are allowed, what attributes are allowed with certain tags and in which order the tags are allowed to be written or laid out in the document.
Like (X)HTML, an XML element is the combination of opening tag, the content and the closing tag. An XML document can contain several kinds of statements. Most common of these are the data elements of the document. XML documents may also include markup declarations and processing instructions.
All XML documents begin with an XML declaration, which is like a processing instruction but is not one. The XML declaration identifies the document as being XML and provides the version number of XML standard and the encoding scheme being used if any as shown below:
<?xml version = “1.0” encoding = “utf-8” ?>
Comments in XML are same as in (X)HTML as shown below:
<!– This is a comment in XML –>
XML names are used for creating tags and attributes. An XML name must start with a letter or an underscore and can include digits, hyphens and periods (dots). XML names are case-sensitive i.e., student and Student are different in XML.
Every XML document must contain a root element, whose opening tag must be the first line of XML code. All other XML elements are nested inside the root element. XML tags are same as in (X)HTML, tag-name surrounded by angular brackets as shown below:
Every XML element that can have content should have a closing tag as shown below:
Elements that do not include any content can be closed as shown below:
XML tags can have attributes, which can be specified as name-value assignments. All the attribute values must be enclosed in double quotes as shown below:
<tag-name attr1=”value” attr2=”value”>content</tag-name>
An XML document that strictly follows all the above mentioned rules are said to be well formed.
When designing a XML document, the designer is often faced with the choice between adding a new attribute to an element or defining a nested element. The situation in which an attribute must be always preferred over a nested element is when identifying numbers or names of elements, exactly as the id and name attributes are used in (X)HTML. Finally, attributes should be used if there is no substructure or if it is really just information about that element and nested elements should be used if the data can be structured. Below is an example of a simple well formed XML document:
<?xml version = "1.0" encoding = "utf-8" ?>