0% found this document useful (0 votes)
11 views5 pages

XMLques

The document outlines exercises for XML, XPath, XSLT, and XQuery, focusing on creating and validating XML documents, navigating XML with XPath, transforming XML to HTML using XSLT, and writing XQuery expressions. It includes specific tasks such as creating a library system XML document, validating it with an XML Schema, and performing various queries and transformations. Additionally, there are advanced challenges involving data transformation and a comparison between XML Schema and DTD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

XMLques

The document outlines exercises for XML, XPath, XSLT, and XQuery, focusing on creating and validating XML documents, navigating XML with XPath, transforming XML to HTML using XSLT, and writing XQuery expressions. It includes specific tasks such as creating a library system XML document, validating it with an XML Schema, and performing various queries and transformations. Additionally, there are advanced challenges involving data transformation and a comparison between XML Schema and DTD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

XML, XPath, XSLT, and XQuery Exercise

Sheet
Section 1: XML Exercises
Exercise 1: Creating an XML Document

Create an XML document representing a library system. The document should include the
following details:

 Library Name
 Books (with Title, Author, Genre, and Year of Publication)
 Members (with Member ID, Name, and Join Date)

Example Structure:

<Library>
<LibraryName>City Central Library</LibraryName>
<Books>
<Book>
<Title>The Great Gatsby</Title>
<Author>F. Scott Fitzgerald</Author>
<Genre>Fiction</Genre>
<Year>1925</Year>
</Book>
<!-- Add more books -->
</Books>
<Members>
<Member>
<MemberID>101</MemberID>
<Name>John Doe</Name>
<JoinDate>2023-01-15</JoinDate>
</Member>
<!-- Add more members -->
</Members>
</Library>

Exercise 2: Validating XML with a Schema

Create an XML Schema (XSD) to validate the XML document you created in Exercise 1. The
schema should:

 Define simple elements like Title, Author, and Genre.


 Use attributes for unique identifiers like MemberID.
 Ensure the Year is a valid four-digit number.

Questions to Answer:
 What are the benefits of using an XML Schema?
 How does validation improve data integrity?

Section 2: XPath Exercises


Exercise 3: Navigating XML with XPath

Given the following XML document:

<Company>
<Employees>
<Employee>
<Name>John White</Name>
<Position>Manager</Position>
<Salary>30000</Salary>
</Employee>
<Employee>
<Name>Ann Beech</Name>
<Position>Assistant</Position>
<Salary>25000</Salary>
</Employee>
</Employees>
</Company>

Write XPath expressions to:

1. Select all Employee names.


2. Retrieve the Salary of the Manager.
3. Find Employees with a Salary greater than 25000.
4. Count the total number of Employees.

Exercise 4: Using Predicates in XPath

Modify the XPath expressions from Exercise 3 to include conditions:

 Select Employees with a Position that contains the word "Manager."


 Retrieve the Name of the Employee with the highest Salary.
 Return the Position of Employees who are not Assistants.

Section 3: XSLT Exercises


Exercise 5: Transforming XML to HTML
Create an XSLT stylesheet to transform the XML document from Exercise 1 into an HTML
table. The table should display:

 Book Titles and Authors


 Member Names and Join Dates

Example XSLT Template:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Library Books</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
</tr>
<xsl:for-each select="Library/Books/Book">
<tr>
<td><xsl:value-of select="Title"/></td>
<td><xsl:value-of select="Author"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Exercise 6: Conditional Transformation in XSLT

Add conditional logic to the XSLT stylesheet created in Exercise 5 to:

 Highlight books published before 2000.


 Display a message if no books are available in the XML document.

Section 4: XQuery Exercises


Exercise 7: Basic XQuery Queries

Use the following XML document for XQuery exercises:

<School>
<Students>
<Student>
<Name>Mary Jane</Name>
<Grade>A</Grade>
</Student>
<Student>
<Name>Peter Parker</Name>
<Grade>B</Grade>
</Student>
</Students>
</School>

Write XQuery expressions to:

1. Retrieve all Student names.


2. Find Students with a Grade of "A."
3. Return a list of Students sorted by Name.

Exercise 8: Using FLWOR Expressions in XQuery

Using the XML document from Exercise 7, write XQuery expressions using FLWOR to:

 Return the Name and Grade of each Student.


 Filter Students who have a Grade less than "B."
 Calculate the number of Students in the XML document.

Section 5: Advanced Challenge Exercises


Exercise 9: Combining XPath and XQuery

Given the XML document from Exercise 1, use both XPath and XQuery to:

 Find all Books authored by "J.K. Rowling."


 Return the Library Name and the number of Books available.
 Retrieve all Member Names who joined in 2023.

Exercise 10: Real-World Scenario - Data Transformation

Create an XML document representing an online store's product catalog. Include:

 Products (with ProductID, Name, Category, Price, and Stock Availability)


 Customers (with CustomerID, Name, and Purchase History)

Then, use XPath, XSLT, and XQuery to:

1. Retrieve Products in a specific Category.


2. Transform the Product list into an HTML table.
3. Generate a report showing Customers and their total purchase amounts.
Submission Guidelines:
 Complete each exercise and save your code files with appropriate names (e.g.,
library.xml, library.xsd, books.xsl, students.xq).
 Submit a zip folder containing all your files.
 Ensure your XML documents are well-formed and valid.

Bonus Challenge:
Research the differences between XML Schema (XSD) and Document Type Definition (DTD).
Create a table comparing their features and provide a brief report on when to use each.

You might also like