Reputation: 61
I have a problem with XQuery. The logic:
Obtain the names of all teachers starting with "Manuel", eliminating repetitions and indicating all classrooms in each teacher's class. Order by name from the shortest to the longest.
I have done this:
for $x in (doc("LMSGI06")//course[starts-with(teacher, "Manuel")])
order by string-length($x/teacher)
return
<teacher>
<name>{data(distinct-values($x/teacher))}</name>
<classroom>{data($x/classroom)}</classroom>
</teacher>`
This is the result of my query:
<teacher>
<name>Manuel Gómez</name>
<classroom>2</classroom>
</teacher>
<teacher>
<name>Manuel Gómez</name>
<classroom>3</classroom>
</teacher>
<teacher>
<name>Manuela Berenguel</name>
<classroom>1</classroom>
</teacher>
<teacher>
<name>Manuel Antonio López</name>
<classroom>1</classroom>
</teacher>
The structure of the xml is this:
<shop>
<education>
<course id="1">
<teacher>Manuela Berenguel</teacher>
<classroom>1</classroom>
</course>
</education>
</shop>
I need Manuel Gómez to appear only once, but I don´t know how. I´m using distinct-values()
but it´s not working. Can someone help me, please?
Upvotes: 2
Views: 40
Reputation: 66724
distinct-values()
isn't helping in your current script, because you are applying it to each individual teacher name as you are generating the element. So, there will only be one value at a time and distinct-values()
returns the current value being processed.
In order to de-duplicate the names, you would want to apply distinct-values()
to all of the teacher element values. Then, you need to decide how to represent the multiple classroom values in your output. The following would produce multiple classroom elements if there are multiple courses for that teacher.
let $manuel-courses := doc("LMSGI06")//course[starts-with(teacher, "Manuel")]
for $teacher in distinct-values($manuel-courses/teacher)
order by string-length($teacher)
return
<teacher>
<name>{data($teacher))}</name>
{$manuel-courses[teacher = $teacher]/classroom)}
</teacher>`
Upvotes: 1