w3reference home
XML Tutorial


Bookmark and Share

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:
  • Prolog
    • XML Declaration
    • DTD Declaration
  • Root Element (Document)
    • Elements (Nested Elements)
      • Element Attributes and Values
      • Data
  • Now we will have a look at each of these parts separately.

    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.

  • The XML declaration tells what version of XML you are using. Now a days the most popular version being used is 1.0. The declaration is not necessary, but it should be included. The XML declaration is written as:
    <?xml version="1.0"?>
  • The Document Type Declaration (DTD) is a file that contains the necessary rules that the XML code in your file must follow. DTD is like the grammar that the XML document must abide by to be a valid XML file. Users can also create their own DTD file. The DTD is written as:
    <encoding="ISO-8859-1"?>

    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:

    <root>
     <child>
       <subchild>.....</subchild>
     </child>
    </root> 
    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:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <book>
    <title>XML for Dummies</title>
    <chapter>Introduction to XML
    <para>Markup Languages</para>
    <para>Features of XML</para>
    </chapter>
    <chapter>XML Syntax
    <para>Elements must be enclosed in tags</para>
    <para>Elements must be properly nested</para>
    </chapter>
    </book>
    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.


    Comments in XML

    Comments in XML are written in a manner similar to HTML. Following is the syntax of a comment:
    <!--Put your comment here.-->
    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:

    < <
    > >
    & &
    ' '
    " "
    Remember: White spaces remain as they are in XML unlike HTML where white spaces are truncated to represent a single white space.
  • Code Validator
    Learn FTP
    Color finder
    Link Checker
    Free web designs
    Coming soon!
    Interview Questions...
    'w3reference : Learn by examples ... Advanced to beginner's tutorials ...'
    Ajax: AJAX tutorial1 | Apache: Apache HTTP Server | Restarting Apache | CSS: CSS Border | CSS Syntax | CSS Selector | CSS Comment | CVS: CVS Release | CVS Login | CVS Logout | CVS Annotate | Databases: Rolap Tutorial | OLAP Tutorial | OLTP Tutorial | data warehousing | Expect: HTML: html | Linux: Dot (.) conf files | Linux Mount Point | Linux Filesystem | SSH Tutorial | Linux Commands: cal | cat | cfdisk | chroot | MySQL: MySQL Commands | PHP: PHP Basics | PHP Variables | PHP Output (echo/print) | PHP String Concat | PL/SQL: PL/SQL Data Types | PL/SQL Control Structures | PL/SQL File Extensions | PL/SQL DBMS_OUTPUT package | Python: My first Python program | Shell: Starting Bash | Bash Redirection | Bash Pipes | Bash Variables | SQL: SQL Transactions | SQL Constraints | SQL Drop | SQL Union & Union All | SVN: svn architecture | SVN Repository | SVN Import | SVN Checkout | Tech: soap | Web Designing: Web Hosting | HTML/XHTML/CSS code validator | Learn FTP | Search Engine Optimization Tips | www: XML: XML vs HTML | XML Syntax | XML Tags, Elements and Attributes | XML Namespaces |
    Sitemap | Disclaim | Privacy Policy | Contact | ©2007-2009 w3reference.com All Rights Reserved.