XSLT 2.0 generates an error: "context element is undefined"

We use Altova Stylevision, which creates XSLT 2.0 files. We use Saxon 9 for Java to execute these XSLT files. This has been working well for several years now, but none of us understands XSLT.

Now we have an 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 

9th function:

 <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 know what is going on?

+6
source share
3 answers

The problem is that the function uses path expressions, such as item , which need a context element as a specification mandate . Inside the body of a stylesheet function, the focus is initially undefined, which means that any attempt to reference a context element, context position or context size is dynamic error that cannot be repaired. [XPDY0002]. " Thus, the function must have a parameter that passes in the node or a sequence of nodes to which the path should apply, for example

 <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 you need to call, for example. sps:GoogleChartDataSourceUnitCount(.) .

If a style sheet is created by some tool from Altova, you can ask in the Altova forums whether this is a known issue and whether a fix is ​​available.

+8
source

According to the W3C XSLT 2.0 specification, the initial context element for xsl:function is undefined .

This means that inside the function body, any reference to data (elements) can occur only with parameters (passed or global) or variables.

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

 concat(string-join(item ... 

and this clearly violates the above rule - item refers to a context element that is not valid inside xsl:function .

Solution :

  • Pass the assigned context element as a parameter (recommended), for example, with the name pDoc , or you have a global variable / parameter containing the intended context element.

  • Link elements in the first step of the XPath expression location disable this parameter / variable, for example $pDoc/item

FAQ Why is this a limitation?

The answer . By not allowing the implicit initial context element, the XSLT processor can perform more extensive static analysis and optimize the code much more aggressively.

+7
source

You may encounter this problem from various use cases. For me, since I forgot to put a dollar sign ($) in front of the parameter inside my function, so the processor thinks that I use node-tag without specifying the context, and then gives this error. I just need to put $ in front of my parameter. Hope my solution will be helpful to others.

+3
source

Source: https://habr.com/ru/post/912829/


All Articles