Xquery for-if-then-else unexpected output



I am trying to return some output and am I'm having some difficulties, it may very well be I don't have the correct understanding of Xquery for-if-then--else statements.



for $S in doc("prac5a.xml")/enrolment return <enrolment>{for $s in//student, $e in//enrol return if($s/id = $e/stud) then <student>{$s/id, $s/name, $e/crs}</student> else <student>{$s/id, $s/name}</student>}</enrolment>


From this I was expecting an output of something like;



<enrolment>
<student>
<id>s1</id>
<name>John</name>
<crs>c1</crs>
<crs>c2</crs>
</student>
<student>
<id>s2</id>
<name>Peter</name>
<crs>c2</crs>
<crs>c3</crs>
</student>
<student>
<id>s3</id>
<name>Rob</name>
</student>


Instead I get this;



<?xml version="1.0" encoding="UTF-8"?>
<enrolment>
<student>
<id>s1</id>
<name>John</name>
<crs>c1</crs>
<crs>c2</crs>
</student>
<student>
<id>s1</id>
<name>John</name>
</student>
<student>
<id>s2</id>
<name>Peter</name>
</student>
<student>
<id>s2</id>
<name>Peter</name>
<crs>c2</crs>
<crs>c3</crs>
</student>
<student>
<id>s3</id>
<name>Rob</name>
</student>
<student>
<id>s3</id>
<name>Rob</name>
</student>
</enrolment>


Why is that? or more importantly I suppose how could I get the desired result.


No comments:

Post a Comment