Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

As per the W3C XSLT 2.0 specification, the initial context item for an xsl:function is undefined.

This means that inside the function body any reference to data (items) can happen only off parameters (passed or global) or variables.

The problem is that an expression in the provided code starts with:

concat(string-join(item ...

and this clearly violates the above rule -- item is referenced off a context item, which isn't permitted inside of an xsl:function.

The solution:

  1. Pass the intended context item as a parameter (recommended) say named pDoc, or have a global variable/parameter contain the intended context item.

  2. Reference items in the first location step of an XPath expression off that parameter/variable, for example $pDoc/item

FAQ: Why is this limitation?

Answer: Not allowing an implicit initial context item makes it possible for the XSLT processor to perform more extensive static analisis and to optimize the code much more agressively.

You may encounter this problem from different use cases. For me, since I forgot putting dollar ($) sign before the param inside my function, so the processor thinks that I am using a node-tag without indicating context, then gives this error. I simply need to put $ before my param. Hope my solution be helpful to others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top