w3reference home
XML Tutorial


Bookmark and Share

Tags, Elements and Attributes

An XML document is made up of tags just like HTML. The tags that compose the XML document are known as elements. Elements are used to classify data in an XML document so that the data becomes "self-explanatory". Let's understand what an element is.
  • All XML elements come in pairs. Hence, each element must have a closing tag. Have a look at the code given below:
    A new paragraph will start now.<P>
    New paragraph has started.
    This code is very much valid in HTML but not in XML. With XML each and every element must have a closing tag.
  • XML elements are case sensitive, i.e. start and end tags must be written in the same case. Look at the example given below:
    <Mail>This mail will not reach the recepient.</mail>
    <mail>This mail will reach the recepient.</mail>
  • XML elements must be properly nested. Look at the code given below:
    <P><b>This is invalid in XML.</P></b>
    <P><b>This is valid in XML.</b></P>
    The first statement will be valid in HTML but XML will give an error. Hence, the correct statement is the second one. It is best to use proper nesting in HTML also to avoid mistakes.
  • XML documents must have a root element. The first element of an XML document is the root element that contains all the other elements. It starts at the beginning and closes at the ending of the document. Hence the last line of an XML document always denotes the end of the XML document.
  • XML does not have any preserved words. The element name can contain alphabets, numbers or any other character but should start with an alphabet. The only exception to the rule is that the word "xml" should not be present at the beginning. The element name should not contain any white spaces.
  • XML elements are extensible. It can often be extended without breaking applications.


    XML attributes define elements. They provide extra information about the element that is not really relevant to the data. Let's take an example of the XML document discussed earlier and add an attribute in it.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <book category="computer">
    <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>
    Here, we have added an attribute, "category" to the element, "book". One thing to note here is that attribute value is written in quotes-either single or double. Also remember that the attributes are written in the start tag of the element. Hence, an attribute is something that defines the element but is not the data itself. It is used because it might help the programmer or the system to understand the XML document.

    Have a look at the XML document given below:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <book>
    <category>computer</category>
    <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>
    This is also very much valid and provides the same information.

    So what is the difference between an element and an attribute?
    The difference between an element and an attribute is in their definition and the way you want your information to be interpreted. There is no such definite rule as to when to use them. Any of them can be used but there are certain drawbacks associated with attributes and hence the reason why a programmer generally avoids it. Some of them are:

  • Attributes cannot contain multiple values.
  • Attributes cannot contain tree structures.
  • Attributes are not easily expandable.
  • Attributes are difficult to read and maintain.
    Hence, use elements for data and use attributes for information that is not relevant to the data but is needed by the system or the programmer.
  • 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.