xsltxpathapply-templates

How to get an attributes value within a XSLT template parameter?


I am calling apply-templates as follows:

<xsl:apply-templates>
    <xsl:with-param name="pWeight" select="@layoutWeight * $pContentWidth" />
</xsl:apply-templates>

I am trying to access the layoutWeight attribute value of the child element to which apply-templates is acting on. However, the computation is always evaluating as empty, even though the child items have a numeric value for layoutWeight and pContentWidth is also a numeric value.

How do I access the layoutWeight attribute of the child element apply-templates is acting on?


Solution

  • The select attribute on a with-param is evaluated relative to the surrounding context, not relative to each node on which templates are being applied. One way to accomplish what you are trying to do is wrap the apply-templates in a for-each:

    <xsl:for-each select="node()">
      <xsl:apply-templates select=".">
        <xsl:with-param name="pWeight" select="@layoutWeight * $pContentWidth" />
      </xsl:apply-templates>
    </xsl:for-each>
    

    The other option would be to just pass the $pContentWidth parameter into the apply-templates and have the template(s) themselves access the @layoutWidth and perform the computation.