XQuery: returning a sequence of multiple elements
Posted by jamie on 24 May 2011 in Notes
Remember the comma operator when returning a sequence of multiple elements in an XQuery return statement. If you want to return more than one element, then you need to wrap the whole thing in parentheses and separate each element with a comma. Compare the first "if" return with the second "else" return:
<div id="relatedList">
{
let $relatedCount := count($relatedNationality)
return
if ($relatedCount < 1) then
<h2>There are no other people from {$nationality}.</h2>
else
(<h2>Other people from {$nationality}.</h2>,
<ul>
{
for $related in $relatedNationality
let $relatedId := $related/@xml:id
let $relatedTitle := $related//tei:title
return
<li>
<a href="player.xql?id={$relatedId}">
<img src="images/thumbs/{$relatedId}.png" alt="{$relatedTitle}"/>
<span class="relatedListItemLine">{$relatedTitle}</span>
</a>
</li>
}
</ul>)
}
</div>