• Oh the embarrassment, thanks to Carsten Dauguaard for spotting it.

    exec sp_xml_preparedocument @iDocumentHandle OUTPUT, @stdoc

    should be

    exec sp_xml_preparedocument @iDocumentHandle OUTPUT, @stXML