If I understand what you’re askcing why not just use “copy-of” to express your nodeset in the code.
e.g.
var $results = somefunc();
copy-of $results;
Then when you run the script pipe it through “|display xml”. (I'm assuming this is on a Junos device, not juise on a host.)
Or did I misinterpret the question?
/doug