Web Technology I · Unit 10
XML and Related Technologies
Exam-focused notes for XML and Related Technologies (Web Technology I, BIT251): what the TU syllabus asks and how it has actually been tested, with 13 solved past questions from this unit.
XML and Related Technologies in short
XML stores and carries data in a plain text format that both people and programs can read. This chapter covers XML rules, schemas that define its structure, and the languages used to search and transform it.
What this unit covers
- XML definition and purpose
- XML extensibility concept
- XML elements and attributes
- XML naming rules
- XML namespaces and name conflict resolution
- CDATA and PCDATA in DTD
- DTD definition and syntax
- XSD schema definition and validation
- DTD versus XSD comparison
- XPath query language
- XQuery language and expressions
- XSLT transformation to HTML
XML elements and attributes
2081 · 10 marks
Explain the XML syntax rules.Create a XML file that contains a root element named and “Office”. Inside this root element create four "Employee" elements. Each "Employee" element must contain a "Skills" element and it can contain up to Three "Skills" elements. Now create an XML file for this scenario and write XSD showing the use of minOccurrence and maxOccurrence indicators.[5+5]
Show solved answerHide answer
XML Syntax Rules and XSD with minOccurs/maxOccurs
(a) XML Syntax Rules
XML (eXtensible Markup Language) follows strict syntax rules. The key rules are:
1. Every XML Document Must Have a Root Element
All elements must be contained within a single root element.
<root> ... </root>
2. All Elements Must Have a Closing Tag
Unlike HTML, every opened tag must be properly closed.
<name>John</name> <!-- correct -->
<name>John <!-- incorrect -->
3. XML Tags are Case-Sensitive
<Employee> and <employee> are treated as different tags.
<Employee>John</Employee> <!-- correct -->
<Employee>John</employee> <!-- incorrect -->
4. Elements Must be Properly Nested
Tags must be opened and closed in the correct order (no overlapping).
<b><i>text</i></b> <!-- correct -->
<b><i>text</b></i> <!-- incorrect -->
5. XML Attribute Values Must be Quoted
All attribute values must be enclosed in single or double quotes.
<Employee id="101"> <!-- correct -->
<Employee id=101> <!-- incorrect -->
6. XML Declaration (Optional but Recommended)
The XML declaration should appear at the top of the document.
<?xml version="1.0" encoding="UTF-8"?>
7. Special Characters Must Use Entity References
Reserved characters must be escaped:
| Character | Entity Reference |
|---|---|
< | < |
> | > |
& | & |
" | " |
' | ' |
8. Comments Syntax
<!-- This is a comment -->
9. White Space is Preserved
Unlike HTML, XML preserves white space within element content.
10. XML Names Cannot Start with a Number or Special Character
Tag names must begin with a letter or underscore.
(b) XML File for the Scenario
XML File (office.xml)
<?xml version="1.0" encoding="UTF-8"?>
<Office>
<Employee id="E001">
<Name>Alice Johnson</Name>
<Department>Development</Department>
<Skills>Java Programming</Skills>
<Skills>Database Management</Skills>
<Skills>Web Development</Skills>
</Employee>
<Employee id="E002">
<Name>Bob Smith</Name>
<Department>Design</Department>
<Skills>UI Design</Skills>
<Skills>Photoshop</Skills>
</Employee>
<Employee id="E003">
<Name>Carol White</Name>
<Department>Testing</Department>
<Skills>Manual Testing</Skills>
<Skills>Automation Testing</Skills>
<Skills>Performance Testing</Skills>
</Employee>
<Employee id="E004">
<Name>David Brown</Name>
<Department>Management</Department>
<Skills>Project Management</Skills>
</Employee>
</Office>
Note:
- Employee E001 has 3 Skills (maximum)
- Employee E002 has 2 Skills
- Employee E003 has 3 Skills (maximum)
- Employee E004 has 1 Skill (minimum)
XSD File (office.xsd) - Showing minOccurs and maxOccurs
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Definition of the root element "Office" -->
<xs:element name="Office">
<xs:complexType>
<xs:sequence>
<!-- Employee element must appear exactly 4 times -->
<xs:element name="Employee" minOccurs="4" maxOccurs="4">
<xs:complexType>
<!-- Attribute for Employee ID -->
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:sequence>
<!-- Name element must appear exactly once -->
<xs:element name="Name"
type="xs:string"
minOccurs="1"
maxOccurs="1"/>
<!-- Department element must appear exactly once -->
<xs:element name="Department"
type="xs:string"
minOccurs="1"
maxOccurs="1"/>
<!-- Skills element must appear at least 1 time
and at most 3 times -->
<xs:element name="Skills"
type="xs:string"
minOccurs="1"
maxOccurs="3"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Explanation of minOccurs and maxOccurs
| Indicator | Meaning | Used In XSD Above |
|---|---|---|
minOccurs | Minimum number of times an element must appear | Skills must appear at least 1 time |
maxOccurs | Maximum number of times an element can appear | Skills can appear at most 3 times |
minOccurs="0" | Element is optional | Not used here |
maxOccurs="unbounded" | Element can repeat unlimited times | Not used here |
Key Points:
minOccurs="1"and `maxO
XSLT transformation to HTML
2081 · 5 marks
Create a XML document with some elements and write equivalent XSLT to render XML content in HTML. [5]
XQuery language and expressions
2080 · 5 marks
What is Xquery? Given following XML, write an XQuery to retrieve the title of movies having budget greater than 500. Write result of the XQuery expression. [5]
XML definition and purpose
2080 · 5 marks
What is the use of XML? Why is it called extensible, justify with example. [5]
Show solved answerHide answer
XML: Uses and Extensibility
What is XML?
XML (eXtensible Markup Language) is a markup language designed to store, transport, and describe data in a structured, human-readable, and machine-readable format. It is a W3C recommended standard.
Uses of XML
| Use | Description |
|---|---|
| Data Storage | Stores structured data in plain text files (e.g., configuration files) |
| Data Exchange | Transfers data between different systems and platforms (e.g., web services, APIs) |
| Web Development | Used in technologies like XHTML, RSS, SOAP, and AJAX |
| Database Communication | Acts as a medium to exchange data between databases |
| Document Representation | Represents documents with complex structure (e.g., Microsoft Office .docx) |
| Configuration Files | Many applications use XML for configuration (e.g., web.xml in Java) |
Why is XML Called "Extensible"?
XML is called extensible because:
Unlike HTML, which has a fixed, predefined set of tags, XML allows users to define their own custom tags according to their specific needs.
There is no fixed vocabulary in XML. You can create any tag name that is meaningful to your application. This ability to extend and define new markup makes it "extensible."
Justification with Example
HTML (NOT extensible - fixed tags only):
<p>John</p>
<b>Engineer</b>
You cannot create
<student>or<rollno>in HTML.
XML (Extensible - user-defined tags):
Suppose a school wants to store student data. They can define their own tags:
<?xml version="1.0" encoding="UTF-8"?>
<school>
<student>
<rollno>101</rollno>
<name>Ram Sharma</name>
<faculty>BSc CSIT</faculty>
<semester>5</semester>
</student>
<student>
<rollno>102</rollno>
<name>Sita Thapa</name>
<faculty>BSc CSIT</faculty>
<semester>5</semester>
</student>
</school>
Now suppose a hospital needs to store patient data. They define completely different tags:
<?xml version="1.0" encoding="UTF-8"?>
<hospital>
<patient>
<patientid>P001</patientid>
<name>Hari Bahadur</name>
<disease>Fever</disease>
<doctor>Dr. Laxmi</doctor>
</patient>
</hospital>
Key Observations:
- Tags like
<rollno>,<faculty>,<patient>,<disease>are not predefined anywhere. - The user created them based on their own requirements.
- Both examples are valid XML despite having completely different tag sets.
Summary
XML is called extensible because it does not restrict users to a fixed set of tags. Users can invent, define, and use their own tags to describe any kind of data, making XML flexible and adaptable to any domain or application.
DTD versus XSD comparison
XML extensibility concept
2079 · 10 marks
What is the meaning of extensible in XML? Mention the XML naming rules. Create an XML file containing an empty element with content, element with element, and element with attribute. Now write equivalent DTD.[10]
XML namespaces and name conflict resolution
2079 · 5 marks
Justify with an example that how XML namespace provides a method to avoid name conflict. [5]
XSD schema definition and validation
2079 · 5 marks
Create a XMl file containing elements Username, Password. Write its XSD and set the restriction of the password must be minimum 4 characters and maximum 8 characters [5]
2080.2 · 10 marks
What is the use of XML?, Given following XML, write XSD for validating the XML and also write XSLT to transform the XML into HTML file. The Name, and Address are string type, Number is integer and the Accessible is Boolean type. The child elements should appear in the order as below.[10]
Show solved answerHide answer
XML, XSD, and XSLT - Model Answer
Part 1: Uses of XML (2 marks)
XML (eXtensible Markup Language) is a markup language designed to store and transport data in a structured, human-readable, and machine-readable format.
Key Uses of XML:
| Use | Description |
|---|---|
| Data Storage | Stores structured data independently of software and hardware |
| Data Exchange | Transfers data between different systems and platforms |
| Web Services | Used in SOAP-based web services for communication |
| Configuration Files | Many applications use XML for configuration (e.g., web.xml, pom.xml) |
| Document Representation | Represents documents with complex structure (e.g., XHTML, SVG) |
| Database Integration | Acts as a medium to exchange data between databases |
| RSS Feeds | Used to syndicate web content |
Part 2: Given XML
Based on the question description, the assumed XML is:
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<Contact>
<Name>Ram Sharma</Name>
<Address>Kathmandu</Address>
<Number>9841000001</Number>
<Accessible>true</Accessible>
</Contact>
<Contact>
<Name>Sita Thapa</Name>
<Address>Pokhara</Address>
<Number>9856000002</Number>
<Accessible>false</Accessible>
</Contact>
</Contacts>
Part 3: XSD (XML Schema Definition) for Validating the XML (4 marks)
An XSD defines the structure, data types, and constraints that an XML document must follow.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Root Element -->
<xs:element name="Contacts">
<xs:complexType>
<xs:sequence>
<!-- Contact element can appear one or more times -->
<xs:element name="Contact" maxOccurs="unbounded">
<xs:complexType>
<!-- Child elements must appear in this exact order -->
<xs:sequence>
<!-- Name: string type -->
<xs:element name="Name" type="xs:string"/>
<!-- Address: string type -->
<xs:element name="Address" type="xs:string"/>
<!-- Number: integer type -->
<xs:element name="Number" type="xs:integer"/>
<!-- Accessible: boolean type -->
<xs:element name="Accessible" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Key Points:
xs:sequenceenforces the order of child elements (Name → Address → Number → Accessible)xs:stringvalidates text dataxs:integervalidates whole numbersxs:booleanvalidatestrueorfalsevaluesmaxOccurs="unbounded"allows multiple<Contact>entries
Part 4: XSLT to Transform XML into HTML (4 marks)
XSLT (eXtensible Stylesheet Language Transformations) is used to transform XML documents into other formats such as HTML, plain text, or another XML.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Match the root of the document -->
<xsl:template match="/">
<html>
<head>
<title>Contact List</title>
<style>
table { border-collapse: collapse; width: 60%; }
th, td { border: 1px solid black; padding: 8px; text-align: left; }
th { background-color: #4CAF50; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<h2>Contact Information</h2>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Number</th>
<th>Accessible</th>
</tr>
<!-- Loop through each Contact element -->
<xsl:for-each select="Contacts/Contact">
<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="Address"/>
</td>
<td>
<xsl:value-of select="Number"/>
</td>
<td>
<xsl:value-of select="Accessible"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Resulting HTML Output
When the XSLT is applied to the XML, the browser renders:
| Name | Address | Number | Accessible |
|---|---|---|---|
| Ram Sharma | Kathmandu | 9841000001 | true |
| Sita Thapa | Pokhara | 9856000002 | false |
Summary
| Component | Purpose |
|---|---|
| XML | Stores structured contact data |
| XSD | Validates data types and element order |
| XSLT | Transforms XML into a readable HTML table |
XPath query language
CDATA and PCDATA in DTD
0 · 5 marks
What is DTD? How can you use CDATA and PCDATA in DTD? [5]
Make Unit 10 stick
Practice BIT251 with flashcards & quizzes