Search code examples
xmlxpathxquery

XPath and XQuery Equivalency


I'm doing some self-study and came across this answer to a problem that I cannot understand.

Given the following DTD:

<!DOCTYPE Courses [
   <!ELEMENT Courses (Course*)>
   <!ELEMENT Course (Subject, Students)>
   <!ATTLIST Course CID ID #REQUIRED Credits PCDATA #REQUIRED>
   <!ELEMENT Subject (#PCDATA)>
   <!ELEMENT Students (Student+)>
   <!ELEMENT Student EMPTY>
   <!ATTLIST Student Name PCDATA #REQUIRED> ]>

[1] I have a XPath prompt:

/Courses/Course[Students/Student/@Name="Smith"]/Subject

[2] And XQuery prompt:

for $c in /Courses/Course
where every $s in $c/Students/Student satisfies $s/@Name="Smith"
return $c/Subject

The answer declares that the result of [2] is always contained in [1]. Is there an intuitive explanation to this?

Based on my understanding, I would suppose that the 2 prompts will output an identical result.


Solution

  • Your XPath expression [1] selects a course if ANY student on the course is named "Smith".

    Your XQuery expression [2] selects a course if EVERY student on the course is named "Smith".

    So they are not equivalent.

    The proposition that the result of [2] is always a subset of [1] relies on the fact that the DTD does not allow a course to have no students, or a student to have no name. If this were not the case, then an empty course would be selected by [2] but not by [1].