XSLT 2.0产生错误:"上下文项未定义"

Adr*_*ith 6 xslt altova saxon xslt-2.0

我们使用Altova Stylevision生成XSLT 2.0文件.我们使用Saxon 9 for Java来执行这些XSLT文件.这已经好几年了,唉,我们都没有真正了解XSLT.

现在我们有错误:

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

第9个功能是:

<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>
Run Code Online (Sandbox Code Playgroud)

有谁知道发生了什么事?

Mar*_*nen 10

问题是该函数使用路径表达式,如item需要一个上下文项,因为规范要求 "在样式表函数的主体内,焦点最初是未定义的;这意味着任何尝试引用上下文项,上下文位置或上下文size是不可恢复的动态错误.[XPDY0002]".因此,该函数需要具有传递在应该应用路径的节点或节点序列中的参数,例如

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

然后需要用例如调用sps:GoogleChartDataSourceUnitCount(.).

如果样式表是由Altova的某个工具生成的,您可能需要在Altova论坛中查询这是否是已知问题以及是否有可用的修复程序.


Dim*_*hev 7

根据W3C XSLT 2.0规范,an的初始上下文项xsl:function是未定义的.

这意味着在函数体内部,对数据(项)的任何引用都只能在参数(传递或全局)或变量之外发生.

问题是提供的代码中的表达式以:

concat(string-join(item ...
Run Code Online (Sandbox Code Playgroud)

这明显违反了上述规则 - item引用了一个上下文项,这是不允许的xsl:function.

解决方案:

  1. 将预期的上下文项作为参数(推荐)传递给named pDoc,或者让全局变量/参数包含预期的上下文项.

  2. 例如,在该参数/变量的XPath表达式的第一个位置步骤中引用项目 $pDoc/item

常见问题:为何这是限制?

:不允许隐式初始上下文项使XSLT处理器能够执行更广泛的静态分析并更加积极地优化代码.