1

I have been struggling to return the count of courses from this XML file that contain "Cross-listed" as their description. The problem I encounter is because I am using for, it iterates and gives me "1 1" instead of "2". When I try using let instead I get 13 which means it counts all without condition even when I point return count($c["Cross-listed"]. What am I doing wrong and how can I fix it? Thanks in advance

for $c in doc("courses.xml")//Department/Course
where some $desc in $c/Description
satisfies contains($desc, "Cross-listed")
return count($c)
Pitagora
  • 47
  • 1
  • 9

1 Answers1

2

The problem I encounter is because I am using for

You are quite correct. You don't need to process items individually in order to count them.

You've made things much too difficult. You want

count(doc("courses.xml")//Department/Course[Description[contains(., "Cross-listed"]])

The key thing here is: you want a count, so call the count() function, and give it an argument which selects the set of things you want to include in the count.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I didn't know that you can use the contains function in XPath like that. That makes it super simple, but what if I wanted to solve it using where and satisfies contains, is it still possible? Thanks! – Pitagora Jun 17 '18 at 21:34
  • 2
    @Pitagora, as Michael said: "call the count() function, and give it an argument which selects the set of things you want to include", so if you want to use for `for` expression you need to call `count` on the complete expression `count(for $c in doc("courses.xml")//Department/Course where ... return $c)` – Martin Honnen Jun 18 '18 at 10:31