XML Course-Catalog Query Exercises


In these exercises (and the companion data exercises), you will be working with a sample data set drawn from the Stanford course catalog. There are multiple departments, each with a department chair, some courses, and professors and/or lecturers who teach courses. Two different versions are provided: the first (courses-noID.xml) encodes the data without using ID and IDREF(S) attributes, and the second (courses-ID.xml) uses ID and IDREF(S).

For more information on how to test your XPath, XQuery, and XSLT solutions, please see our quick guide to XML validation and querying.


XPath

Write each of the following queries using XPath. Write each query twice -- once for each version of the data (courses-noID.xml and courses-ID.xml). We suggest that you work with the noID version first, since some queries are more difficult on the ID version.

1.  Return last names of all department chairs.

2.  Return all Title elements (of both departments and courses).

3.  Return titles of courses with enrollment greater than 500.

4.  Return titles of departments that have some course that takes "CS106B" as a prerequisite.

5.  Return the course number of the course that is cross-listed as "LING180".

6.  Return last names of all professors or lecturers who use a middle initial. Don't worry about eliminating duplicates.

7.  Return course numbers of courses that have the same title as some other course. (Hint: You might want to use the "preceding" and "following" navigation axes for this query, which were not covered in the video or our demo script; they match any preceding or following node, not just siblings.)

8.  Return course numbers for courses taught by an instructor with first name "Daphne" or "Julie".


XPath and XQuery

Write each of the following queries using both XPath and XQuery. Write each query three times -- once in XPath on the noID version of the data, then using XQuery constructs on both the noID and ID versions of the data. (XPath on the ID version is difficult or impossible for some of these queries.) For XQuery, we suggest that you start with the noID version of the data.

9.  Return the count of courses that have a cross-listed course (i.e., that have "Cross-listed" in their description).

10.  Return the count of courses that have no lecturers as instructors.

11.  Return titles of courses taught by the chair of a department. For this question, you may assume that all professors have distinct last names.

12.  Return last names of instructors teaching at least one course that has "system" in its description and enrollment greater than 100.

13.  Return titles of courses taught by both lecturers and professors, preferably returning each title only once.

14.  Return titles of courses taught by a professor with the last name "Ng" but not by a professor with the last name "Thrun".

15.  Return course numbers for courses that have a course taught by Eric Roberts as a prerequisite.


XQuery


Write each of the following queries using XQuery. Write each query twice -- once for each version of the data. Once again, we suggest that you start with the noID version of the data.

16.  Return the average enrollment of all courses in the CS department.

17.  Return the title of the course with the largest enrollment.

18.  Compile a course catalog summary, listing all CS department courses in order of enrollment. For each course include only its Enrollment (as an attribute) and its Title (as a subelement).

19.  Create a Professors listing of all professors in all departments, sorted by last name. For this question, you may assume that all professors have distinct last names. Do not include InstrID's in the courses-ID.xml version of the data set. Watch out: middle names may require some special handling.

20.  Building on the previous question, create an inverted course catalog: list professors with the courses they teach, sorted by last name. You may still assume that all professors have distinct last names. Make a single "Courses" subelement under each Professor, containing a further subelement for each course number taught by that professor. For an extra challenge, professors who do not teach any courses should have no Courses subelement at all.


XSLT


Write each of the following queries using XSLT. Write each query once, using only the noID version of the data.

21.  Return a list of department titles.

22.  Return a list of department elements with no attributes and two subelements each: the department title and the last name of the department chair.

23.  Return a list of department elements with no attributes and two subelements each: the department title and the entire Chair subelement structure.

24.  Return those courses with enrollment less than 100. Retain the structure of Course elements from the original data.

25.  Remove from the data all courses with enrollment less than 100 and all courses with no enrollment listed. Otherwise the structure of the data should be the same.

26.  Create a summarized version of the course catalog: for each course, include its number and title as attributes, its description as a sublement, and a subelement for enrollment if provided. Preserve the department structure, but discard all information about department titles, chairs, instructors, and prerequisites.

27.  Create a table using HTML constructs that lists all CS department courses with enrollment greater than 100. Each row should contain the course number in italics, course title in bold, and enrollment. Sort the rows alphabetically by title. No header is needed. (Note: To specify quotes within an already-quoted XPath expression, use quot;.)