XML is a language for storing and exchanging data in a form that both people and programs can read. Unlike HTML, it lets authors invent their own tags.

Introduction and Uses

XML (eXtensible Markup Language) is a markup language, recommended by the W3C in 1998, for storing and transporting data. HTML describes how data looks, while XML describes what the data is. There are no predefined tags: authors create tags that suit their data, such as <student> and <mobile>. XML is used for data exchange between systems, configuration files (Android layouts, Maven pom.xml), document formats (DOCX and XLSX are zipped XML), RSS news feeds, SVG graphics, and web services (SOAP).

Definition: XML. eXtensible Markup Language, a W3C markup language for storing and transporting data using self-describing, user-defined tags arranged in a tree.

Key Concepts and Rules

An XML document begins with the XML declaration, <?xml version="1.0" encoding="UTF-8"?>, and has exactly one root element that contains all others. Elements may have attributes, written in quotes. A well-formed document follows the syntax rules: every start tag has an end tag (or is self-closing, <br/>), tags are case-sensitive, elements are properly nested, attribute values are quoted, and special characters are written as entities (<, &). A valid document is well-formed and follows the rules of a DTD or schema. Namespaces prevent clashes between tag names from different vocabularies.

DTD (Document Type Definition)

A DTD defines the legal structure of an XML document: which elements are allowed, in what order, how often and with what attributes. <!ELEMENT students (student+)> says that students contains one or more student elements. <!ELEMENT student (regno, name, mobile, dob, email)> gives the child elements in order. <!ELEMENT name (#PCDATA)> says that name contains text. <!ATTLIST student id CDATA #REQUIRED> declares an attribute. The symbols are + (one or more), (zero or more) and ? (optional). A DTD can be internal, inside <!DOCTYPE>, or external*, in a .dtd file.

XML Schema (XSD)

An XML Schema (XSD) does the same job as a DTD, but is itself written in XML and is more powerful. It supports data types, such as xs:string, xs:integer and xs:date, restrictions such as patterns and lengths, and exact occurrence limits with minOccurs and maxOccurs. For example, a schema can require the mobile number to be exactly 10 digits, which a DTD cannot.

Table 4.4: DTD and XML Schema

Feature DTD XML Schema (XSD)
Syntax Special DTD syntax XML itself
Data types Text only (#PCDATA) Many (string, integer, date)
Restrictions Limited Patterns, lengths, ranges
Namespaces Not supported Supported

Example 4.4: Student information in XML with a DTD and schema

Problem: Write an XML file for students with register number, name, mobile number, date of birth and email, with an internal DTD, and give a schema fragment that checks the mobile number.

Solution:

  1. Start with the XML declaration and a DOCTYPE containing the DTD.
  2. Make <students> the root, containing <student> elements with the five children.
  3. In the schema, restrict mobile to the pattern [0-9]{10} and dob to xs:date.

Result: A valid, self-describing student record file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students [
  <!ELEMENT students (student+)>
  <!ELEMENT student (regno, name, mobile, dob, email)>
  <!ELEMENT regno (#PCDATA)>  <!ELEMENT name (#PCDATA)>
  <!ELEMENT mobile (#PCDATA)> <!ELEMENT dob (#PCDATA)>
  <!ELEMENT email (#PCDATA)>
]>
<students>
  <student>
    <regno>2526BCA001</regno> <name>Asha Sharma</name>
    <mobile>9876543210</mobile> <dob>2007-04-12</dob>
    <email>asha@example.com</email>
  </student>
</students>
<!-- Schema fragment (students.xsd):
  <xs:element name="mobile">
    <xs:simpleType><xs:restriction base="xs:string">
      <xs:pattern value="[0-9]{10}"/>
    </xs:restriction></xs:simpleType>
  </xs:element>
  <xs:element name="dob" type="xs:date"/>              -->

Common mistakes

  • Leaving a tag unclosed or changing its case, which makes XML not well-formed.
  • Having more than one root element.
  • Thinking a well-formed document is always valid; validity needs a DTD or schema.

Key takeaways

  • XML stores and transports data with user-defined tags.
  • Well-formed: one root, closed and nested tags, quoted attributes, case-sensitive.
  • Valid: well-formed and conforming to a DTD or schema.
  • XSD is written in XML and supports data types and restrictions.

Practice questions

  1. What is the main purpose of XML?
  2. Distinguish well-formed and valid XML.
  3. What does + mean in a DTD element declaration?

Answers to the practice questions

  1. To store and transport data in a self-describing form.
  2. Well-formed XML follows the syntax rules; valid XML also conforms to a DTD or schema.
  3. One or more occurrences.

Sources

  • Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit IV