XSL:分配的位置()的结果,以一个可变

问题描述:

我试图分配一个元件的这样的位置:XSL:分配的位置()的结果,以一个可变

<xsl:variable name="offset" select="ancestor::myparent/position() * 8"/> 

我试图示出了在输出计算的结果,所以使用选择器是不够的。 xsltproc生成以下错误:

XPath error : Invalid expression 
ancestor::myparent/position() * 8 
          ^
compilation error: file foo.xsl line 10 element variable 
XSLT-variable: Failed to compile the XPath expression 'ancestor::myparent/position() * 8'. 

有没有办法做到这一点?

position()仅在引用特定上下文时可用。

当您处理myparent元素时,您应该设置该变量。

<xsl:template match="myparent"> 
    <xsl:variable name="offset" select="position() * 8"/> 
    <xsl:apply-templates> 
    <xsl:with-param name="offset" select="$offset"/> 
    </xsl:apply-templates> 
</xsl:template> 

就像上面那样。确保向需要偏移量的模板添加一个参数。