XSLT Error Reference

XPST0008

All versions XPath static error
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

  1. Scope confusion — the #1 cause. A variable declared inside xsl:if, xsl:when or xsl:for-each disappears 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"/>
  1. Typo in the variable name ($totlaPrice).
  2. Template parameters not declared. Using $foo in a template that never declares <xsl:param name="foo"/> — declare the param even if the caller sends it with xsl: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.