[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: calculating math:matrix longest column strings

Subject: RE: calculating math:matrix longest column strings
From: Jarno.Elovirta@xxxxxxxxx
Date: Thu, 16 Oct 2003 10:01:35 +0300
math matrix
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.