PHPOffice/PHPWord · error · PhpOffice\PhpWord\Exception\Exception

Could not transform the given XML document.

Error message

Could not transform the given XML document.

What it means

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.

Solutions

  1. Ensure the XSL is XSLT 1.0 (libxslt does not support 2.0+)
  2. Check XPath expressions match the actual XML namespaces of the document parts (declare xmlns:x=... in the stylesheet and use x: prefixes)
  3. Capture libxml errors via libxml_get_errors() around the transform to see the underlying reason
  4. Test the stylesheet against the extracted XML with xsltproc first

Example fix

// before
<!-- XPath without namespace prefix over namespaced docx XML -->
<xsl:value-of select="//w:p" />
// after
<xsl:stylesheet xmlns:x="http://schemas.openxmlformats.org/wordprocessingml/2006/main" ...>
  <xsl:value-of select="//x:p" />
Defensive patterns

Strategy: try-catch

Validate before calling

// validate the stylesheet compiles and is XSLT 1.0
$xsl = new DOMDocument();
$xsl->loadXML($xslString);
$proc = new XSLTProcessor();
if (!$proc->importStylesheet($xsl)) {
    throw new RuntimeException('XSL stylesheet invalid (use XSLT 1.0)');
}

Try / catch

try {
    $processor->applyXslStyleSheet($xslDom);
} catch (\PhpOffice\PhpWord\Exception\Exception $e) {
    if ($e->getMessage() === 'Could not transform the given XML document.') {
        // check XPath/namespace prefixes in the stylesheet
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/76edd3fbf5444e4f. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/TemplateProcessor.php:201

    /**
     * @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 mixed
     */
    protected function transformXml($xml, $xsltProcessor)
    {
        if (is_array($xml)) {
            foreach ($xml as &$item) {

View on GitHub (pinned to aef95c0415)