{"record":{"id":"76edd3fbf5444e4f","repo":"PHPOffice/PHPWord","slug":"could-not-transform-the-given-xml-document","errorCode":null,"errorMessage":"Could not transform the given XML document.","messagePattern":"Could not transform the given XML document\\.","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/TemplateProcessor.php","lineNumber":201,"sourceCode":"    /**\n     * @param string $xml\n     * @param XSLTProcessor $xsltProcessor\n     *\n     * @return string\n     */\n    protected function transformSingleXml($xml, $xsltProcessor)\n    {\n        if (\\PHP_VERSION_ID < 80000) {\n            $orignalLibEntityLoader = libxml_disable_entity_loader(true);\n        }\n        $domDocument = new DOMDocument();\n        if (false === $domDocument->loadXML($xml)) {\n            throw new Exception('Could not load the given XML document.');\n        }\n\n        $transformedXml = $xsltProcessor->transformToXml($domDocument);\n        if (false === $transformedXml) {\n            throw new Exception('Could not transform the given XML document.');\n        }\n        if (\\PHP_VERSION_ID < 80000) {\n            libxml_disable_entity_loader($orignalLibEntityLoader);\n        }\n\n        return $transformedXml;\n    }\n\n    /**\n     * @param mixed $xml\n     * @param XSLTProcessor $xsltProcessor\n     *\n     * @return mixed\n     */\n    protected function transformXml($xml, $xsltProcessor)\n    {\n        if (is_array($xml)) {\n            foreach ($xml as &$item) {","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/TemplateProcessor.php#L183-L219","documentation":"TemplateProcessor::transformSingleXml applies the XSLTProcessor to the loaded DOMDocument; when transformToXml returns false the transformation itself failed and 'Could not transform the given XML document.' is thrown. The stylesheet compiled, but the transform could not produce output.","triggerScenarios":"applyXslStyleSheet() with an XSL stylesheet whose XPath expressions do not match the document structure, produce runtime errors, or with an invalid/incomplete stylesheet that importStylesheet accepted but cannot execute.","commonSituations":"XSL written for a different document schema than the .docx parts; XSLT 2.0 features unsupported by libxslt (PHP's XSLTProcessor only supports XSLT 1.0); XPath functions not available in libxslt.","solutions":["Ensure the XSL is XSLT 1.0 (libxslt does not support 2.0+)","Check XPath expressions match the actual XML namespaces of the document parts (declare xmlns:x=... in the stylesheet and use x: prefixes)","Capture libxml errors via libxml_get_errors() around the transform to see the underlying reason","Test the stylesheet against the extracted XML with xsltproc first"],"exampleFix":"// before\n<!-- XPath without namespace prefix over namespaced docx XML -->\n<xsl:value-of select=\"//w:p\" />\n// after\n<xsl:stylesheet xmlns:x=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ...>\n  <xsl:value-of select=\"//x:p\" />","handlingStrategy":"try-catch","validationCode":"// validate the stylesheet compiles and is XSLT 1.0\n$xsl = new DOMDocument();\n$xsl->loadXML($xslString);\n$proc = new XSLTProcessor();\nif (!$proc->importStylesheet($xsl)) {\n    throw new RuntimeException('XSL stylesheet invalid (use XSLT 1.0)');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $processor->applyXslStyleSheet($xslDom);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if ($e->getMessage() === 'Could not transform the given XML document.') {\n        // check XPath/namespace prefixes in the stylesheet\n    }\n}","preventionTips":["Test the XSL with xsltproc against the extracted document part first","Declare and use the docx namespace prefix in all XPath expressions","Stick to XSLT 1.0 — libxslt (PHP) does not support 2.0/3.0","Capture libxml_get_errors() to diagnose transform failures"],"tags":["xml","xslt","template"],"backgroundTag":"xml-parse-error","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}