Output generated by a template gets written to the standard output stream unless the template was called from inside <xsl:variable>, in which case the output become the value of that variable.
Is there a way to get both out of one template, that is, a way to direct some of the output to the standard output stream and direct some of the output back to the calling <xsl:variable>?
Yes. Just capture the complete template output in a variable first. Then process this variable’s content and output the wanted parts. Then create a new variable, whose content has the wanted parts from the complete template output that is captured in the first variable.
It would be good if you could provide a specific XML document and wanted result.