views:

201

answers:

1

I am trying to assign the position of an element like this:

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

I am trying to show the result of the calculation in the output, so using a selector is not enough. xsltproc generates the following error:

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'.

Is there a way to do this?

+1  A: 

position() is available only in reference to a specific context.

When you process the myparent element, you should set the variable then.

<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>

Something like the above. Make sure to add a param to the templates that need the offset.

Kyle Butt