PHPOffice/PHPWord · error · PhpOffice\PhpWord\Exception\Exception
Could not set values for the given XSL style sheet…
Error message
Could not set values for the given XSL style sheet parameters.
What it means
TemplateProcessor::applyXslStyleSheet creates an XSLTProcessor, imports the given XSL stylesheet, and calls setParameter to register $xslOptions; if setParameter returns false it throws 'Could not set values for the given XSL style sheet parameters.' before any transformation runs.
Solutions
- Pass a flat associative array of string keys to string values: ['paramName' => 'value']
- Ensure parameter names are valid XSL names (no spaces/special characters)
- Cast values to string before passing
- Check the stylesheet expects those params (xsl:param) with matching names
Example fix
// before $processor->applyXslStyleSheet($xsl, [['a','b']]); // indexed array // after $processor->applyXslStyleSheet($xsl, ['a' => 'b']);
Defensive patterns
Strategy: validation
Validate before calling
function validXslOptions(array $opts): bool {
foreach ($opts as $k => $v) {
if (!is_string($k) || !preg_match('/^[A-Za-z_][A-Za-z0-9_.-]*$/', $k) || !is_scalar($v)) return false;
}
return true;
}
// use: if (!validXslOptions($xslOptions)) { $xslOptions = []; } Type guard
function isStringMap($v): bool {
return is_array($v) && array_is_list($v) === false
&& count(array_filter($v, 'is_scalar')) === count($v);
} Try / catch
try {
$processor->applyXslStyleSheet($xslDom, $opts);
} catch (\PhpOffice\PhpWord\Exception\Exception $e) {
if (str_contains($e->getMessage(), 'XSL style sheet parameters')) {
$processor->applyXslStyleSheet($xslDom, []); // retry without params
}
} Prevention
- Pass a flat string-keyed => scalar-valued array
- Cast all option values to string
- Ensure parameter names match <xsl:param name="..."> in the stylesheet
When it happens
Trigger: Passing $xslOptions that is not a valid key/value array (e.g. indexed array, nested arrays, non-string values), causing XSLTProcessor::setParameter to reject it.
Common situations: Passing an associative array with mixed-type values, numerically indexed arrays, or options read from JSON/config in unexpected shape; forgetting that keys become XSL parameter names which must be valid names.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Could not transform the given XML document.
- Could not load the given XML document.
- Invalid image
- Unsupported image type $imageMimeType
- Can not clone row, template variable not found or variable…
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/a1045eb329774719.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/TemplateProcessor.php:246
}
/**
* Applies XSL style sheet to template's parts.
*
* Note: since the method doesn't make any guess on logic of the provided XSL style sheet,
* make sure that output is correctly escaped. Otherwise you may get broken document.
*
* @param DOMDocument $xslDomDocument
* @param array $xslOptions
* @param string $xslOptionsUri
*/
public function applyXslStyleSheet($xslDomDocument, $xslOptions = [], $xslOptionsUri = ''): void
{
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet($xslDomDocument);
if (false === $xsltProcessor->setParameter($xslOptionsUri, $xslOptions)) {
throw new Exception('Could not set values for the given XSL style sheet parameters.');
}
$this->tempDocumentHeaders = $this->transformXml($this->tempDocumentHeaders, $xsltProcessor);
$this->tempDocumentMainPart = $this->transformXml($this->tempDocumentMainPart, $xsltProcessor);
$this->tempDocumentFooters = $this->transformXml($this->tempDocumentFooters, $xsltProcessor);
}
/**
* @param string $macro
*
* @return string
*/
protected static function ensureMacroCompleted($macro)
{
if (substr($macro, 0, 2) !== self::$macroOpeningChars && substr($macro, -1) !== self::$macroClosingChars) {
$macro = self::$macroOpeningChars . $macro . self::$macroClosingChars;
}
View on GitHub (pinned to aef95c0415)