CSS Prepare

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

  1. Exactly one root element.
  2. Every start tag has a matching end tag (or is self-closing: ``).
  3. Tags are case-sensitive (<Book> ≠ <book>).
  4. Elements properly nested (no overlapping).
  5. Attribute values always quoted.
  6. Special characters escaped (&lt; &gt; &amp; &apos; &quot;).
  7. 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"/>
PointDTDXSD
SyntaxIts own non-XML syntaxWritten in XML
Data typesMostly text onlyRich types (integer, date, decimal, patterns)
NamespacesNot supportedSupported

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

PointXMLHTML
PurposeStore/transport dataDisplay data
TagsUser-definedPredefined
SyntaxStrict; case-sensitive; all tags closedLenient; case-insensitive
ErrorsParser stops on errorBrowser tries to render anyway

XHTML vs XML (CSS 2018)

PointXHTMLXML
NatureA specific XML application: HTML reformulated with XML rulesA meta-language for creating any markup language
TagsFixed set of HTML tagsAny user-defined tags
PurposePresenting web pages in browsersDescribing, storing and exchanging data
SchemaFixed DTDs (Strict, Transitional, Frameset)Any DTD/XSD, or none
Rendered byBrowsers directlyNeeds stylesheet/transformation or an application
RelationshipEvery XHTML document is an XML documentNot every XML document is XHTML

XML vs JSON

PointXMLJSON
VerbosityVerbose (opening and closing tags)Compact
Data typesEverything is text (unless schema)Strings, numbers, booleans, arrays, objects, null
FeaturesAttributes, namespaces, schemas, comments, XSLTSimple; native to JavaScript
Typical useSOAP, documents, configs, RSSREST 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

YearQuestion
2018Differentiate 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.

BasisXHTMLXML
Full formeXtensible HyperText Markup LanguageeXtensible Markup Language
PurposeDisplaying web pages (a stricter, XML-based version of HTML)Describing and transporting data, independent of display
TagsPredefined (<p>, <div>, <a>)User-defined (<student>, <price>)
RulesMust be well-formed XML: lowercase tags, all tags closed and properly nested, attribute values quotedMust be well-formed, and may be validated with a DTD or XML Schema
DisplayBrowsers render it directlyNo display meaning, needs CSS, XSLT or a program
UseWeb pagesData 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>