{"record":{"id":"9a657a2488726c1e","repo":"PHPOffice/PHPWord","slug":"could-not-load-the-given-xml-document","errorCode":null,"errorMessage":"Could not load the given XML document.","messagePattern":"Could not load the given XML document\\.","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/TemplateProcessor.php","lineNumber":196,"sourceCode":"        }\n\n        return $this->fixBrokenMacros($this->zipClass->getFromName($fileName));\n    }\n\n    /**\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","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/TemplateProcessor.php#L178-L214","documentation":"TemplateProcessor::transformSingleXml loads a document part's XML into a DOMDocument before applying an XSL transform; if loadXML fails (malformed XML), it throws 'Could not load the given XML document.'. On PHP < 8.0 entity loading is disabled around the call, so documents relying on external entities may also fail.","triggerScenarios":"applyXslStyleSheet() on a template whose header/footer/main part XML is corrupt or truncated; a template edited by hand and saved with invalid XML; entity-based XML that libxml refuses to load on PHP < 8.","commonSituations":"Corrupted .docx templates; templates edited in non-XML-aware tools; programmatically injected replacement strings that break XML (unescaped & or <); PHP version differences around libxml_disable_entity_loader.","solutions":["Validate the template's XML parts are well-formed (try loading them with DOMDocument before processing)","Escape/encode injected replacement text with htmlspecialchars or XML-safe encoding","Open the .docx, extract the part, and run xmllint to pinpoint the malformed element","Re-export the template from Word/LibreOffice if it was hand-edited"],"exampleFix":"// before\n$str = str_replace('&', '&', $raw); // wrong escaping\n// after\n$str = htmlspecialchars($raw, ENT_XML1);\n$templateProcessor->setValue('name', $str);","handlingStrategy":"validation","validationCode":"function xmlPartIsValid(string $xml): bool {\n    libxml_use_internal_errors(true);\n    $doc = new DOMDocument();\n    $ok = $doc->loadXML($xml);\n    libxml_clear_errors();\n    return $ok;\n}","typeGuard":null,"tryCatchPattern":"try {\n    $processor->applyXslStyleSheet($xsl);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if ($e->getMessage() === 'Could not load the given XML document.') {\n        // template corrupt: re-export template, escape injected strings\n    }\n}","preventionTips":["Escape all replacement values with htmlspecialchars(…, ENT_XML1)","Never hand-edit .docx XML with non-XML-aware editors","Keep templates generated by Word/LibreOffice, not pasted together","Validate template XML parts in CI before shipping them"],"tags":["xml","template","docx"],"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"}