Hi,
suppose I have a math:matrix of 4 columns by 4 rows (but the matrix
could be of any size). I am currently "looking at" a math:mn
in the 2nd
row, 3rd column during processing of an XSLT transform. I need to be
able to find and sum the longest cell in each of the preceding two
columns. By the "longest" cell, I mean the cell which has
the longest
string in the column. In other words, I need to add together the
lengths of the longest strings in all the preceding columns of the
current element being processed. The structure of the matrix
conforms
to the w3c schema for mathML, so use any example you like such as
[snip]
So, for purposes of this example, the answer would be 4.
How about something like
<xsl:template match="/">
<xsl:for-each select="math/matrix/matrixrow[2]/*[3]">
<xsl:call-template name="summer"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="summer">
<xsl:param name="i" select="count(preceding-sibling::*)"/>
<xsl:param name="sum" select="0"/>
<xsl:choose>
<xsl:when test="$i > 0">
<xsl:variable name="longest">
<xsl:for-each select="../../matrixrow/*[$i]">
<xsl:sort select="string-length(.)" data-type="number"
order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="string-length(.)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:call-template name="summer">
<xsl:with-param name="i" select="$i - 1"/>
<xsl:with-param name="sum" select="$sum + $longest"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
You can increase the performance by using some other technique to find the
longest string, e.g. recursive template. Dimitre's FXSL can also help you here.
Cheers,
Jarno - Nick Sentience: March 2003 Mix <http://hardnrg.com/music.php>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list