mrteeth
mrteeth

Reputation: 153

Filtering by attribute

Below is a excerpt from a XML file with 65 lectures:

   <?xml version="1.0" encoding="iso-8859-1" ?>
   <university>
    <lecture>
        <class>English</class>
        <hours>3</hours>
        <pupils>30</pupils>
    </lecture>
    <lecture>
        <class>Math</class>
        <hours>4</hours>
        <pupils>27</pupils
    </lecture>
    <lecture>
        <class>Science</class>
        <hours>2</hours>
        <pupils>25</pupils>
    </lecture>
   </university>

I need a where clause that gives me a list of lectures with more pupils than an English lecture. However, not with the attribute "30" used, but calling the English's lecture attribute instead

E.g., I want to use a where clause with a condition like pupils > English.pupils, instead of pupils > 30.

(The "pupils > English.pupils" is just puesdo code as an example)

Upvotes: 2

Views: 115

Answers (1)

wst
wst

Reputation: 11771

A where clause isn't strictly necessary, but to use one you would make it part of a for iterator:

let $lectures := doc("lectures.xml")/university/lecture
let $english-pupils := $lectures[class = "English"]/pupils/xs:integer(.)
for $lecture in $lectures
where ($lecture/pupils/xs:integer(.) gt $english-pupils)
return $lecture

You could also avoid the flwor altogether by using an XPath predicate.

let $lectures := doc("lectures.xml")/university/lecture
let $english-pupils := $lectures[class = "English"]/pupils/xs:integer(.)
return $lectures[pupils/xs:integer(.) gt $english-pupils]

Upvotes: 5

Related Questions