PHPOffice/PHPWord · error · PhpOffice\PhpWord\Exception\Exception
Could not load the given XML document.
Error message
Could not load the given XML document.
What it means
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.
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
Example fix
// before
$str = str_replace('&', '&', $raw); // wrong escaping
// after
$str = htmlspecialchars($raw, ENT_XML1);
$templateProcessor->setValue('name', $str); Defensive patterns
Strategy: validation
Validate before calling
function xmlPartIsValid(string $xml): bool {
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$ok = $doc->loadXML($xml);
libxml_clear_errors();
return $ok;
} Try / catch
try {
$processor->applyXslStyleSheet($xsl);
} catch (\PhpOffice\PhpWord\Exception\Exception $e) {
if ($e->getMessage() === 'Could not load the given XML document.') {
// template corrupt: re-export template, escape injected strings
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not transform the given XML document.
- Can not clone row, template variable not found or variable…
- Can not delete row , template variable not found or…
- Can not find the start position of the table.
- Can not find the start position of the row to clone.
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/9a657a2488726c1e.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/TemplateProcessor.php:196
}
return $this->fixBrokenMacros($this->zipClass->getFromName($fileName));
}
/**
* @param string $xml
* @param XSLTProcessor $xsltProcessor
*
* @return string
*/
protected function transformSingleXml($xml, $xsltProcessor)
{
if (\PHP_VERSION_ID < 80000) {
$orignalLibEntityLoader = libxml_disable_entity_loader(true);
}
$domDocument = new DOMDocument();
if (false === $domDocument->loadXML($xml)) {
throw new Exception('Could not load the given XML document.');
}
$transformedXml = $xsltProcessor->transformToXml($domDocument);
if (false === $transformedXml) {
throw new Exception('Could not transform the given XML document.');
}
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($orignalLibEntityLoader);
}
return $transformedXml;
}
/**
* @param mixed $xml
* @param XSLTProcessor $xsltProcessor
*
* @return mixedView on GitHub (pinned to aef95c0415)