ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

XML, which stands for eXtensible Markup Language, is a markup language designed to store and transport data in a structured and readable format. Unlike HTML, which is used to display data in a browser, XML is used to describe and carry data so that different systems can exchange information with each other. The most important feature of XML is that it is extensible, meaning users can define their own tags according to the needs of their data, rather than being limited to a fixed set of predefined tags.

XML was developed by the World Wide Web Consortium (W3C) as a simpler and more flexible descendant of SGML. It quickly became a standard for data exchange between applications, databases, and web services. For example, when an online shopping website needs to send order information to a bank, XML is often the format used to structure that order data. Because XML separates the data from its presentation, the same XML document can be displayed in many different ways using XSLT or processed by programs in many languages.

In this chapter, we will study the structure of an XML document, the rules for writing well-formed XML, the concept of elements and attributes, and the differences between HTML and XML. We will also learn about DTD and other advanced concepts. By the end of this chapter, students will be able to create simple XML documents that follow all the rules of well-formedness and understand why XML is so important in modern data exchange.

2. Features of XML

XML has several distinguishing features that make it ideal for data storage and transfer.

These features make XML the backbone of many technologies including RSS feeds, web services using SOAP, configuration files, and data interchange between applications.

3. XML Syntax and Rules

XML documents must follow certain rules to be considered well-formed. A well-formed XML document obeys all the syntax rules of XML, and only such documents can be parsed by applications.

Basic rules of a well-formed XML document

  1. XML documents must have a root element that contains all other elements. There can be only one root element.
  2. Every start tag must have a matching end tag. For example, <name> must be closed by </name>.
  3. Tags must be properly nested. <a><b></b></a> is correct, but <a><b></a></b> is wrong.
  4. Attribute values must always be enclosed in quotation marks, either single or double.
  5. XML is case-sensitive. <name> and <Name> are two different tags.
  6. Tags cannot contain spaces or begin with a number. Comments in XML are written as <!-- comment -->.
  7. Empty elements can be written in the shorthand form, such as <br/> or <img/>.

Example of a well-formed XML document

<?xml version="1.0" encoding="UTF-8"?>
<school>
    <student roll="1">
        <name>Riya Sharma</name>
        <class>X</class>
        <marks>92</marks>
    </student>
    <student roll="2">
        <name>Arjun Verma</name>
        <class>X</class>
        <marks>88</marks>
    </student>
</school>

In this document, school is the root element, student is a child element, and roll is an attribute of the student element. The first line is the XML declaration which specifies the version and encoding.

4. Elements and Attributes

In XML, an element is the basic building block of a document. An element consists of a start tag, its content, and an end tag. Elements can contain text, other elements (called child elements), or both. The element that contains other elements is called the parent element, and the contained elements are called child elements. Elements at the same level are called siblings.

Attributes provide additional information about an element. They are written inside the start tag as name-value pairs, for example <student roll="1">. An element can have any number of attributes, but no two attributes in the same tag can have the same name, and each attribute can appear only once in a tag. The choice between using an element or an attribute depends on the nature of the data; data that is part of the content is best kept in an element, while data that describes the content is best kept in an attribute.

5. Differences between HTML and XML

Although HTML and XML are both markup languages, they serve very different purposes.

These differences are frequently tested in examinations, so they should be memorised carefully.

6. XML Technologies

XML is supported by a family of related technologies.

Web services such as SOAP and REST exchange data in XML format, making XML an essential technology for modern web development.

7. Advantages and Limitations of XML

Advantages

Limitations

Quick Revision Tables

Feature HTML XML
Purpose Display data Store and transport data
Tags Predefined tags only User-defined tags allowed
Case sensitivity Not case-sensitive Case-sensitive
Closing tags Not always required Always required
Error handling Browsers tolerate errors Parsers reject malformed documents
Whitespace Collapsed by browsers Preserved
XML Concept Meaning
Root element The single top-level element containing all others
Well-formed Follows all XML syntax rules
Attribute Extra information inside a start tag
DTD Defines the valid structure of a document
XSLT Transforms XML into another format such as HTML
XML declaration <?xml version="1.0"?> at the start

Mind Map

graph TD A["XML Basics"] --> B["Features"] A --> C["Syntax Rules"] A --> D["Elements and Attributes"] A --> E["HTML vs XML"] A --> F["XML Technologies"] B --> B1["Extensible, Self-descriptive"] B --> B2["Platform independent"] C --> C1["One root element"] C --> C2["Matching closing tags"] C --> C3["Quoted attributes"] D --> D1["Parent and child elements"] D --> D2["Attributes give extra info"] F --> F1["DTD, XML Schema"] F --> F2["XSL, XSLT, XPath, RSS"]

Important Diagrams (SVG)

Diagram 1: Structure of an XML Document Tree

Root: <school> <student roll="1"> <student roll="2"> <name> <class> <marks> Attributes such as roll="1" appear inside the start tag. Golden Rule: An XML document can have exactly one root element.

Diagram 2: Well-Formed vs Ill-Formed XML

Well-Formed (Correct) <book> <title>Maths</title> <price>250</price> </book> Properly nested, tags matched Ill-Formed (Wrong) <book> <title>Maths</title> <price>250</book> </price> Cross-nesting of tags (invalid) An XML parser rejects ill-formed documents completely. Golden Rule: Every start tag must have a matching end tag and tags must nest properly.

Common Mistakes

  1. Creating more than one root element in a document. XML permits only a single root element.
  2. Forgetting to close a tag, especially self-closing elements that must be written as <br/>.
  3. Using mixed case inconsistently. Since XML is case-sensitive, <Name> and <name> are treated as different elements.
  4. Writing attribute values without quotation marks, such as <student roll=1> instead of <student roll="1">.
  5. Misplacing the XML declaration. It must be the very first line of the document.
  6. Thinking XML is meant to display data like HTML. XML stores data and requires XSLT or a program to display it.
  7. Starting a tag name with a number, which is not allowed in XML.

Exam Tips

  1. Memorise the differences between HTML and XML in a table, as direct questions are common.
  2. Be able to write a simple well-formed XML document with a root element, two child elements, and at least one attribute.
  3. Remember that attribute values must be quoted and tags must be case-sensitive and properly nested.
  4. Learn the full form of XML and know that it is a W3C recommendation.
  5. Practise identifying well-formed versus ill-formed XML snippets, a frequent two-mark question.
  6. Remember that DTD defines the valid structure of an XML document and XSLT transforms XML into another format.

Conclusion

XML is a powerful, extensible markup language designed for storing and transporting structured data. We learned that XML documents must be well-formed, meaning they must have exactly one root element, properly nested and matched tags, quoted attribute values, and case-sensitive naming. Elements and attributes provide the structure and description of the data, and technologies such as DTD, XSLT, and XPath extend the power of XML. Although HTML and XML both use tags, HTML displays data while XML carries it. This ability to exchange structured data makes XML the foundation of web services, RSS feeds, and countless business applications.