XML Syntax
Now we will have a look at the syntax of an XML document. The general format of an XML document can be represented as:- XML Declaration
- DTD Declaration
- Elements (Nested Elements)
- Element Attributes and Values
- Data
Prolog
The first line of an XML document is the prolog. Although it is optional but it should be included. If included, the prolog must be appear before the root element. A prolog consists of two parts: the XML Declaration and the Document Type Declaration (DTD). Depending on your needs, you can choose to include both, either, or neither of these items in your XML document.
Root
The next part is the root. But before discussing let's know something about XML tags. XML uses tags just like HTML but unlike HTML, it does not have a predefined set of tags. All the tags are user defined. And another important thing to remember is that all the tags are used in pairs i.e. each and every tag should have it's closing tag counterpart also. After the prolog comes tags and the first tag is the root tag. Each XML file must have a root element which starts at the beginning and closes at the end of the document. Root is the parent element of all other elements. Inside the root, there are child elements.
An XML document will look something like:
If you look at the above structure then you can say that an XML document forms a tree structure, that starts at the root and branches to the lower levels......
Take an example of the document given below:
We will try to evaluate this XML document. As you can understand by just looking at the above document, it is a document containing the details of a book named "XML for Dummies". The first line is the prolog consisting of XML declarataion and Document Type Declaration. The next line is the root element of the document. The lines following the root element describe the child elements of the root. The last line is a closing tag that defines the end of the root and hence the XML document.XML for Dummies Introduction to XML Markup Languages Features of XML XML Syntax Elements must be enclosed in tags Elements must be properly nested
Comments in XML
Comments in XML are written in a manner similar to HTML. Following is the syntax of a comment:Although comments can contain almost anything, remember that the sequence "xml" cannot appear inside a comment in any form.
Entity reference in XML
Some characters hold a special meaning in XML and if they are a part of the data, XML won't know. Hence, a set of escape sequence is used for this purpose. Their list is given below:
| < | < |
| > | > |
| & | & |
| ' | ' |
| " | " |
