Add-on to my previous note (below) about returning nodesets to XSLT from PHP functions:
You don't have to return a DOMDocument, DOMElement works just as well. Plus, retuning a DOMElement gets around the problem of discarding the root node which I discuss below and which is also touched on by "Ingram".