------------------------------------------------------------------------
-- Example XPath Expressions from Chapter 9
------------------------------------------------------------------------
--
set long 1024
--
-- Get course numbers of all courses in the catalog.
select extract(gbook,'/gradebook/catalog/entry/cno/text()') as CNO
from gradebook;
--
--
-- Get cno attribute from course elements.
select extract(gbook,'/gradebook/courses/course/@cno') as CNO
from gradebook;
--
--
-- Get course numbers (without <cno> tags) of all courses in the catalog.
select extract(gbook,'/gradebook/catalog/entry/cno/text()') as CNO
from gradebook;
--
--
-- Get all children nodes of <student> element.
select extract(gbook,'/gradebook/students/student/*')
from gradebook;
--
--
-- Get all attributes of <course> element.
select extract(gbook,'/gradebook/courses/course/@*')
from gradebook;
--
--
-- Get third <student> element.
select extract(gbook,'/gradebook/students/student[3]')
from gradebook;
--
--
-- Get last <student> element.
select extract(gbook,'/gradebook/students/student[last()]')
from gradebook;
--
--
-- Get first three <student> elements.
select extract(gbook,'/gradebook/students/student[position()<=3]')
from gradebook;
--
--
-- Get <student> elements with minit value "B".
select extract(gbook,'/gradebook/students/student[minit="B"]')
from gradebook;
--
--
-- Get <component> elements whose weight attribute is > 40.
select extract(gbook,'/gradebook/courses/course/component[@weight>40]')
from gradebook;
--
--
-- Get <student> elements whose last name starts with "Raj".
select extract(gbook,'/gradebook/students/student[starts-with(lname,"Raj")]')
from gradebook;
--
--
-- Get <student> elements whose last name contains substring "she".
select extract(gbook,'/gradebook/students/student[contains(lname,"she")]')
from gradebook;
--
--
-- Get score for student "4444" on grading component "exam1"
-- in the course "csc226" in term "f96" with line number "1031".
select extract(gbook,
'/gradebook/courses/course[@term="f96" and @lineno="1031" and @cno="csc226"]/component[@name="exam1"]/score[@sid="4444"]/text()'
) as score
from gradebook;
--
--
-- Get <student> elements whose last name starts with "R" or "C".
select extract(gbook,
'/gradebook/students/student[starts-with(lname,"R") or starts-with(lname,"C")]'
)
from gradebook;
--
--
-- Get <student> elements whose last name starts with "R" and 
-- first name does not contain substring "dita".
select extract(gbook,
'//student[starts-with(lname,"R") and not (contains(fname,"dita"))]'
)
from gradebook;
--
--
-- Get <course> elements with at least one grading component.
select extract(gbook,
'/gradebook/courses/course[component]'
)
from gradebook;
--
--
-- Get first names of students as well as names of grading components.
select extract(gbook,
'//student/fname/text() | //component/@name'
)
from gradebook;
--
--
-- Get cno attribute value that have a corresponding <course> element.
-- Illustrates use of "axis" in predicate.
select extract(gbook,
'/gradebook/courses/course[@cno=ancestor::gradebook//entry/cno]/@cno'
)
from gradebook;
