XSLT 2.0 produces error: "the context item is undefined"

Adrian Smith picture Adrian Smith · Apr 10, 2012 · Viewed 8.1k times · Source

We use Altova Stylevision which produces XSLT 2.0 files. We use Saxon 9 for Java to execute these XSLT files. This has been working well for a few years, alas none of us actually understand XSLT.

Now we have the error:

Error at /xsl:stylesheet/xsl:function[9]
XPDY0002: Axis step child::element(item, xs:anyType) cannot be used here:
  the context item is undefined

The 9th function is:

<xsl:function name="sps:GoogleChartDataSourceUnitCount" as="xs:string">
  <xsl:sequence select="concat(string-join(item/string(if ( number($XML/report/calculation-data[@data-source-name = $DataSourceParent]/item/variable[@name=&apos;unit_count&apos;]/@value) &lt; 0 ) then 0 else round-half-to-even(number(variable[@name=&apos;unit_count&apos;]/@value),2)),&apos;,&apos;),&apos;&amp;chxl=0:|&apos;,string-join(item/variable[@name=&apos;month&apos;]/@value,&apos;|&apos;),&apos;|2:||Min&amp;chds=0,&apos;,string(round-half-to-even( max(item/(number(variable[@name=&apos;unit_count&apos;]/@value)))+1 , 0 )),&apos;&amp;chxr=1,0,&apos;,string(round-half-to-even( max(item/(number(variable[@name=&apos;unit_count&apos;]/@value)))+1 , 0 )))"/>
</xsl:function>

Does anyone have any idea what's going on?

Answer

Martin Honnen picture Martin Honnen · Apr 10, 2012

The problem is that the function uses path expressions like item which need a context item as the specification mandates "Within the body of a stylesheet function, the focus is initially undefined; this means that any attempt to reference the context item, context position, or context size is a non-recoverable dynamic error. [XPDY0002]". So the function needs to have a parameter that passes in the node or sequence of nodes to which the path should be applied e.g.

<xsl:function name="sps:GoogleChartDataSourceUnitCount" as="xs:string">
  <xsl:param name="nodes"/>
  <xsl:sequence select="concat(string-join($nodes/item/string(...)))"/>
</xsl:function>

and then needs to be called with e.g. sps:GoogleChartDataSourceUnitCount(.).

If the stylesheet is generated by some tool from Altova you might want to enquire in the Altova forums whether this is a known issue and whether a fix is available.