Well after little bit of trial and error, I was able to get the required output -
let $items := ( doc("Subject")//Topic , doc("Classification")//Topic )
for $x in distinct-values($items )
order by $x
return <Subject>{$x}</Subject>
I was confused where to put the distinct-values()
and how to get two documents open in one stroke.