(: Build a portfolio of paper titles per author. Additionally report the session a paper appeared in. Over bibliography.xml. parke godfrey 2013-11-05 This demonstrates the call-by-reference nature of return results. $author points to an author node in the document. In the inner FOR, we can walk back up the tree (/..) to find the session. This would not be possible if the nodes returned by the FOR were call-by-value, and were copied from the document. In bibliography.xml, some paper's parent node is "session", but not every paper is within a session node. The IF...THEN...ELSE... construct checks whether a session was found for tha paper. name() returns the tag's name. :) { for $author in doc("bibliography.xml")//author order by $author return { for $paper at $i in doc("bibliography.xml")//paper[.//author = $author] let $title := $paper/title/text() let $session := $paper/..[name() = 'session']/@name return if (empty($session)) then ( {$title} ) else ( {$title} ) } }