Imagine if I have an xml document stored in Marklogic in the following format:
<document>
<id>DocumentID</id>
<questions>
<question_item>
<question>question1</question>
<answer>answer1</answer>
</question_item>
<question_item>
<important>high</important>
<question>question2</question>
<answer>answer2</answer2>
<question_item>
</document>
Basically, each document has a number of questions, only some of them have an element. I want to return all of the "important" questions in a flat format with metadata taken from the document it is pulled from (e.g., id).
The following xquery seems to work, and is reasonably fast:
for $x in cts:search(/document,
cts:element-query(xs:QName("important"),cts:and-query((
))
), "unfiltered" , 0.0)
return for $y in $x/questions/question_item
return
if ($y/important) then
fn:concat($x/id,'|',
$y/question,'|',
$y/answer,
$y/important
)
else ()
This seems to work and is reasonably fast. However, I usually find that the for loops are not the fastest way to work in xquery. The solution does seem to be a relatively cumbersome approach. Is there a better way to return just the "important" nodes initially, but then still have access to the main document elements?