XSLT Error Reference
XPST0008
Error message
Variable $x has not been declared (or its declaration is not in scope)An XPath expression references a variable that does not exist at that point: a typo, or a variable declared inside a block and used outside its scope.
What it means
The expression uses $x but no xsl:variable or xsl:param named x is visible from that point. In XSLT, a variable’s scope is its parent element, from the point of declaration onward.
Common causes
- Scope confusion — the #1 cause. A variable declared inside
xsl:if,xsl:whenorxsl:for-eachdisappears when that element ends:
<!-- Before: $discount is out of scope at the value-of -->
<xsl:if test="@vip = 'true'">
<xsl:variable name="discount" select="0.2"/>
</xsl:if>
<xsl:value-of select="$discount"/> <!-- XPST0008 -->
<!-- After: declare the variable once, decide the value inside it -->
<xsl:variable name="discount"
select="if (@vip = 'true') then 0.2 else 0"/>
<xsl:value-of select="$discount"/>
- Typo in the variable name (
$totlaPrice). - Template parameters not declared. Using
$fooin a template that never declares<xsl:param name="foo"/>— declare the param even if the caller sends it withxsl:with-param.
How to fix it
Move the declaration up to a scope that encloses every use, or compute conditional values inside the variable (as above). Remember stylesheet-level (global) variables are visible everywhere.