You maybe don't want to start by writing to a file. You want to separate "did the query produce something sensical" and "does the file write do what I expect" as sources of error so you can't try to debug the wrong thing for a few hours.
return
(: file:write($output || $filename, :)local:process-data($data)(: ) :)can be a remarkably useful construct until you like what you're seeing. The BaseX GUI very sensibly truncates the output so you can't crush it.
-- Graydon