RE: [xsl] calculating math:matrix longest column strings

Subject: RE: [xsl] calculating math:matrix longest column strings
From: Jarno.Elovirta@xxxxxxxxx
Date: Thu, 16 Oct 2003 10:01:35 +0300

>   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


> 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:template name="summer">
    <xsl:param name="i" select="count(preceding-sibling::*)"/>
    <xsl:param name="sum" select="0"/>
      <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:call-template name="summer">
          <xsl:with-param name="i" select="$i - 1"/>
          <xsl:with-param name="sum" select="$sum + $longest"/>
        <xsl:value-of select="$sum"/>

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.


Jarno - Nick Sentience: March 2003 Mix <>

 XSL-List info and archive:

Current Thread