You're not far off, but the expression must account for the structure of your input XML:
colspan="{count(//Companies/row[last()]/@*)}"
In your input XML, there are no table
, tr
and td
elements.
I understand that you attempted to count the table cells that are being generated in your XSLT stylesheet, but this information can be retrieved from your input XML as well and it is more straightforward.
Besides, I am wondering why you'd like to count the number of cells dynamically in one place, if you hard-code the attributes that should be turned into table cells in another?
EDIT: It turns out that you intended to ask about the theoretical possibility of accessing nodes of the current stylesheet. Yes, there are ways to do this.
XSLT 1.0
If you are limited to XSLT 1.0, use the document()
function, but without specifying an URI to be resolved:
<xsl:value-of select="count(document('')//td)"/>
Michael Kay's "XSLT 2.0 Programmer's Reference" says:
[document('')] refers to the stylesheet itself. This construct was often used with XSLT 1.0, where it provided a convenient way to maintain look-up tables in the stylesheet itself.
See for example this similar SO question.
XSLT 2.0
Store information about which attributes should be turned into table cells in an xsl:variable
element. Then, access its contents both for generating the table and determining the value of the colspan
attribute.
Actually, this could be done in XSLT 1.0 as well, if you have a means of converting a result tree fragment to a node-set (e.g. with an EXSLT function).