10 Xml And Related Technologies

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.

XML and Related Technologies: notes

XML definition and purpose

XML (eXtensible Markup Language) is a markup language for storing and transporting data. HTML shows data. XML describes what the data is.

<?xml version="1.0" encoding="UTF-8"?>
<student>
  <name>Sita</name>
  <age>20</age>
</student>

Purposes of XML:

  • To store data in a simple text format.
  • To exchange data between different systems.
  • To keep data separate from its presentation.
  • To create new markup languages, such as RSS and SVG.
PointHTMLXML
PurposeDisplay dataDescribe and carry data
TagsPredefinedDefined by the author
CaseNot sensitiveCase sensitive
Closing tagsSometimes optionalAlways required
ErrorsBrowser forgivesParser stops

XML extensibility concept

XML is extensible because the author can invent new tags to suit the data. There is no fixed tag set. The tags can grow as needs change without breaking old programs, which simply ignore tags they do not know.

<book>
  <title>Web Technology</title>
  <author>Rai</author>
  <!-- later we add a new tag -->
  <year>2024</year>
</book>

This is why XML is the base of many other languages such as XHTML, SVG and MathML. Each defines its own tags using XML rules.

XML elements and attributes

An element is made of a start tag, content and an end tag. Elements can hold text, other elements, or be empty like . One root element must contain all others.

An attribute gives extra information inside the start tag as a name and quoted value.

<student id="101" status="active">
  <name>Ram</name>
  <marks subject="Web">85</marks>
</student>

Rules for a well formed document:

  • There is exactly one root element.
  • Every start tag has a matching end tag.
  • Tags are properly nested.
  • Attribute values are in quotes.
  • Tag names are case sensitive.

Elements are better for data. Attributes suit metadata such as ids. An attribute cannot hold many values or a tree structure.

XML naming rules

Names of elements and attributes follow these rules.

  • A name starts with a letter or an underscore.
  • It may contain letters, digits, hyphens, underscores and periods.
  • It cannot contain spaces.
  • It cannot start with the letters xml in any case.
  • Names are case sensitive, so <Name> and <name> differ.
  • Avoid colons, which are used for namespaces.

Valid names: student, _id, first-name. Invalid names: 1name, my name, xmlData.

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 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 -->

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:

CharacterEntity Reference
<&lt;
>&gt;
&&amp;
"&quot;
'&apos;

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

IndicatorMeaningUsed In XSD Above
minOccursMinimum number of times an element must appearSkills must appear at least 1 time
maxOccursMaximum number of times an element can appearSkills can appear at most 3 times
minOccurs="0"Element is optionalNot used here
maxOccurs="unbounded"Element can repeat unlimited timesNot used here

Key Points:

  • minOccurs="1" and `maxO
Open this answer on its own page →

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]

--- --- ID Name Faculty Marks --------------------------------- 1 Ram Sharma CSIT 85 2 Sita Thapa BCA 90 3 Hari Poudel BIT 78 --- Element / Attribute Purpose ------ <?xml-stylesheet? Links the XML file to the XSLT stylesheet <xsl:stylesheet Root element of XSLT; declares XSL namespace <xsl:templa...

Open this answer on its own page →

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]

XQuery is a query and functional language designed to query and transform XML data. It is to XML what SQL is to relational databases. XQuery is built on XPath expressions and can extract, filter, and restructure data from XML documents. Key features of XQuery: - Uses FLWOR expressions: for, let, ...

Open this answer on its own page →

XML definition and purpose

2080 · 5 marks

What is the use of XML? Why is it called extensible, justify with example. [5]

Show solved 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

UseDescription
Data StorageStores structured data in plain text files (e.g., configuration files)
Data ExchangeTransfers data between different systems and platforms (e.g., web services, APIs)
Web DevelopmentUsed in technologies like XHTML, RSS, SOAP, and AJAX
Database CommunicationActs as a medium to exchange data between databases
Document RepresentationRepresents documents with complex structure (e.g., Microsoft Office .docx)
Configuration FilesMany 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.

Open this answer on its own page →

0 · 5 marks

What is the use of XML? Discuss the XML elements. [5]

XML (eXtensible Markup Language) is a markup language designed to store, transport, and describe data in a structured, human-readable, and machine-readable format. Unlike HTML, XML focuses on what data is, not how it looks. --- 1. Data Storage and Transport: XML is widely used to store and exchan...

Open this answer on its own page →

DTD versus XSD comparison

2080 · 5 marks

How DTD is different from XSD? [5]

Note: The reference notes did not contain this topic directly. The following answer is based on standard XML curriculum content appropriate for BSc CSIT. --- DTD is a set of rules/declarations that defines the structure and legal elements of an XML document. It is the older, simpler way to define...

Open this answer on its own page →

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]

--- Extensible in XML means that users can define their own tags (elements) according to their needs. Unlike HTML, which has a fixed set of predefined tags, XML allows you to create custom markup languages tailored to any domain or application. Key points of extensibility: - You can create any ta...

Open this answer on its own page →

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]

An XML Namespace is a mechanism that provides a way to avoid element name conflicts in XML documents by associating element and attribute names with a unique URI (Uniform Resource Identifier). It is declared using the reserved attribute xmlns. Syntax: --- Consider a situation where two different ...

Open this answer on its own page →

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]

--- --- Component Purpose ------ xs:schema Root element of every XSD file xs:complexType Used because <login contains child elements xs:sequence Child elements must appear in order xs:simpleType Defines a custom simple type for Password xs:restriction base="xs:string" Restricts the base type stri...

Open this answer on its own page →

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 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:

UseDescription
Data StorageStores structured data independently of software and hardware
Data ExchangeTransfers data between different systems and platforms
Web ServicesUsed in SOAP-based web services for communication
Configuration FilesMany applications use XML for configuration (e.g., web.xml, pom.xml)
Document RepresentationRepresents documents with complex structure (e.g., XHTML, SVG)
Database IntegrationActs as a medium to exchange data between databases
RSS FeedsUsed 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:sequence enforces the order of child elements (Name → Address → Number → Accessible)
  • xs:string validates text data
  • xs:integer validates whole numbers
  • xs:boolean validates true or false values
  • maxOccurs="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:

NameAddressNumberAccessible
Ram SharmaKathmandu9841000001true
Sita ThapaPokhara9856000002false

Summary

ComponentPurpose
XMLStores structured contact data
XSDValidates data types and element order
XSLTTransforms XML into a readable HTML table
Open this answer on its own page →

0 · 5 marks

Given following XML, write its equivalent XSD. [5]

Since no specific XML was provided in the question, I will use a standard example commonly used in TU BSc CSIT exams: --- --- Component Description ------ xs:schema Root element of every XSD document xs:element Declares an XML element with name and type xs:complexType Used when an element has chi...

Open this answer on its own page →

XPath query language

2080.2 · 5 marks

Discuss the use of XQuery and XPath in XML. [5]

Note: The reference notes did not contain material on this topic. The following answer is based on standard, correct computer science knowledge appropriate for BSc CSIT curriculum. --- XPath is a query language used to navigate through elements and attributes in an XML document. It uses a path-li...

Open this answer on its own page →

CDATA and PCDATA in DTD

0 · 5 marks

What is DTD? How can you use CDATA and PCDATA in DTD? [5]

DTD (Document Type Definition) is a set of rules or declarations that defines the structure, legal elements, attributes, and data types of an XML document. It acts as a blueprint or schema that specifies: - What elements are allowed in the document - How elements can be nested - What attributes e...

Open this answer on its own page →