XML Course-Catalog XPath and XQuery Exercises

In this article we are going to solve the exercises for "XML Course-Catalog XPath and XQuery Exercises".  We are going to use the XPath language. This is my first touch with XPath language so let me know for errors. A nice page to find more about XPath is : http://edutechwiki.unige.ch/en/XPath_tutorial_-_basics#Example:_Computation_of_an_average


Question 1
Return all Title elements (of both departments and courses).
doc("courses.xml")/Course_Catalog//*/Title

Question 2
Return last names of all department chairs.
doc("courses.xml")/Course_Catalog/Department/Chair/Professor/Last_Name
Question 3
Return titles of courses with enrollment greater than 500.
doc("courses.xml")/Course_Catalog/Department/Course[@Enrollment>500]/Title
Question 4
Return titles of departments that have some course that takes "CS106B" as a prerequisite.
doc("courses.xml")/Course_Catalog/Department[Course/Prerequisites/Prereq="CS106B"]/Title

Question 5
Return last names of all professors or lecturers who use a middle initial. Don't worry about eliminating duplicates.
doc("courses.xml")/Course_Catalog/Department//Professor[Middle_Initial]/Last_Name
|
doc("courses.xml")/Course_Catalog/Department//Lecturer[Middle_Initial]/Last_Name

Question 6
Return the count of courses that have a cross-listed course (i.e., that have "Cross-listed" in their description).
count(doc("courses.xml")/Course_Catalog/Department/Course[contains(Description,"Cross-listed")]/Description)

Question 7
Return the average enrollment of all courses in the CS department.
sum(doc("courses.xml")/Course_Catalog/Department[@Code="CS"]/Course/data(@Enrollment)) div count (doc("courses.xml")/Course_Catalog/Department[@Code="CS"]/Course/data(@Enrollment))

Question 8
Return last names of instructors teaching at least one course that has "system" in its description and enrollment greater than 100.
doc("courses.xml")/Course_Catalog/Department/Course[contains(Description,"system") and @Enrollment>100]/Instructors//Last_Name


The last question is a really difficult question. The secret is that we have to search all the departments if they have a course that has an enrollment that has a value smaller than the current. The following-siblings and preceding-siblings cannot help us. We have to look all the XML Tree.
Question 9
Return the title of the course with the largest enrollment.
doc("courses.xml")//Course[@Enrollment and not (@Enrollment > following::*/data(@Enrollment)) and not (@Enrollment > preceding::*/data(@Enrollment))]/Title

Σχόλια

Δημοφιλείς αναρτήσεις