optional · CSS study notes
15. XML (eXtensible Markup Language)
CSS Optional — Paper-II, Section-B · P-II.VI.15 · CSS 2018 asked to differentiate XHTML and XML. XML also appears in the Database section (XML documents & web services); this page focuses on the web-development angle.
1. Definition
- XML is a W3C markup language (Recommendation 1998), derived from SGML, for storing and transporting data in a format that is both human-readable and machine-readable.
- Extensible: there are no predefined tags — you define your own tags that describe the data (self-describing).
- XML carries data; HTML displays data.
2. Example
<?xml version="1.0" encoding="UTF-8"?>
<!-- Library catalogue -->
<library>
<book id="B101" category="CS">
<title>Web Engineering</title>
<author>Kappel</author>
<year>2006</year>
<price currency="PKR">4500</price>
</book>
<book id="B102" category="CS">
<title>Digital Image Processing</title>
<author>Gonzalez</author>
<year>2018</year>
<price currency="PKR">6000</price>
</book>
</library>
3. Rules for Well-Formed XML
- Exactly one root element.
- Every start tag has a matching end tag (or is self-closing: ``).
- Tags are case-sensitive (
<Book>≠<book>). - Elements properly nested (no overlapping).
- Attribute values always quoted.
- Special characters escaped (
<>&'"). - The XML declaration, if present, must be first.
Well-formed vs valid: well-formed follows the syntax rules; valid is well-formed and conforms to a schema (DTD or XSD).
4. DTD and XML Schema (XSD)
<!-- DTD -->
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, year, price)>
<!ATTLIST book id ID #REQUIRED>
<!ELEMENT title (#PCDATA)>
]>
<!-- XSD fragment -->
<xs:element name="year" type="xs:integer"/>
<xs:element name="price" type="xs:decimal"/>
| Point | DTD | XSD |
|---|---|---|
| Syntax | Its own non-XML syntax | Written in XML |
| Data types | Mostly text only | Rich types (integer, date, decimal, patterns) |
| Namespaces | Not supported | Supported |
5. The XML Family
- Namespaces: avoid name clashes (
xmlns:h="..."). - XPath: navigate/select nodes (
/library/book[price>5000]/title). - XSLT: transform XML into HTML or other XML.
- XQuery: query XML collections.
- Parsers: DOM (tree in memory) and SAX (event-based streaming).
6. Uses of XML
Data exchange between systems; configuration files (e.g. Android manifests, Java/Struts configs); RSS/Atom feeds; SOAP web services and WSDL; SVG graphics; XHTML; office formats (DOCX, XLSX are zipped XML); sitemaps (sitemap.xml) for SEO.
7. Comparisons
XML vs HTML
| Point | XML | HTML |
|---|---|---|
| Purpose | Store/transport data | Display data |
| Tags | User-defined | Predefined |
| Syntax | Strict; case-sensitive; all tags closed | Lenient; case-insensitive |
| Errors | Parser stops on error | Browser tries to render anyway |
XHTML vs XML (CSS 2018)
| Point | XHTML | XML |
|---|---|---|
| Nature | A specific XML application: HTML reformulated with XML rules | A meta-language for creating any markup language |
| Tags | Fixed set of HTML tags | Any user-defined tags |
| Purpose | Presenting web pages in browsers | Describing, storing and exchanging data |
| Schema | Fixed DTDs (Strict, Transitional, Frameset) | Any DTD/XSD, or none |
| Rendered by | Browsers directly | Needs stylesheet/transformation or an application |
| Relationship | Every XHTML document is an XML document | Not every XML document is XHTML |
XML vs JSON
| Point | XML | JSON |
|---|---|---|
| Verbosity | Verbose (opening and closing tags) | Compact |
| Data types | Everything is text (unless schema) | Strings, numbers, booleans, arrays, objects, null |
| Features | Attributes, namespaces, schemas, comments, XSLT | Simple; native to JavaScript |
| Typical use | SOAP, documents, configs, RSS | REST APIs, web apps |
Same data in JSON: {"book": {"id": "B101", "title": "Web Engineering", "year": 2006}}
🎯 CSS Exam Angle
Define XML → show a small example → well-formedness rules → DTD/XSD → uses. For comparison questions (XHTML vs XML, XML vs HTML/JSON) the tables above are complete answers; add the one-line relationship "every XHTML document is XML".
📌 Past CSS Paper Frequency
| Year | Question |
|---|---|
| 2018 | Differentiate between XHTML and XML |
⚠️ MCQ Traps
- XML tags are case-sensitive and user-defined.
- Well-formed ≠ valid: valid also conforms to a DTD/XSD.
- XSD is written in XML; DTD is not.
- XSLT transforms XML; XPath selects nodes.
- XML was designed to carry data, not display it.
✅ Self-Check (with Answers)
Q1. Find the errors: <Student><name>Ali</Name><age>21<age></Student><Student>...</Student>
Ans: Case mismatch (name vs Name); age not closed properly (needs </age>); two root elements — wrap them in a single root such as <students>.
Q2. Differentiate well-formed and valid XML.
Ans: Well-formed XML follows syntax rules (one root, closed and properly nested tags, quoted attributes, case consistency). Valid XML is well-formed and also conforms to a DTD or XML Schema.
Q3. Write an XPath to select titles of books cheaper than 5000.
Ans: /library/book[price < 5000]/title
Q4. Why have JSON APIs largely replaced XML in web apps?
Ans: JSON is more compact (less bandwidth), parses natively into JavaScript objects, and supports basic data types directly; XML remains useful where schemas, namespaces, documents or legacy SOAP are required.
Q5. Is every XML document an XHTML document?
Ans: No. XHTML is one particular XML vocabulary with fixed HTML tags; XML can define any tags. Every XHTML document is XML, but not vice versa.
📜 Past-Paper Questions & MCQs (CSS 2016–2026)
2018 · Paper-II · Q8(B) · 6 marks
Q. Differentiate between XHTML and XML.
Ans.
| Basis | XHTML | XML |
|---|---|---|
| Full form | eXtensible HyperText Markup Language | eXtensible Markup Language |
| Purpose | Displaying web pages (a stricter, XML-based version of HTML) | Describing and transporting data, independent of display |
| Tags | Predefined (<p>, <div>, <a>) | User-defined (<student>, <price>) |
| Rules | Must be well-formed XML: lowercase tags, all tags closed and properly nested, attribute values quoted | Must be well-formed, and may be validated with a DTD or XML Schema |
| Display | Browsers render it directly | No display meaning, needs CSS, XSLT or a program |
| Use | Web pages | Data exchange, configuration, web services and RSS feeds |
2022 · Paper-II · Q8(b) · XML part
Q. Discuss XML.
Ans. XML is a text-based, self-describing markup language with user-defined tags. It stores data in a hierarchical (tree) structure, is platform independent, and is used for data exchange, configuration files and web services. A document must be well-formed (one root element, properly nested and closed tags), and can be validated against a DTD or schema.
<student><name>Ali</name><regNo>101</regNo></student>