Q:

Finding the longest cell in a table with XSLT 2.0

Building on this topic:
How do I select an XML node with the longest child #text node value with XPath?
I try to find the longest cell in column 1 of a table. Unfortunately I don't know how many ancestors the table has and there are sometimes several within one text- element which should be treated different.
XML

    <text><table cols="3" rows="2">
        <row >
            <cell >first cell first row</cell>
            <cell >second cell first row
            </cell>
            <cell >third cell first row
                    </cell>
        </row>
        <row >
            <cell >first cell second row</cell>
            <cell >this is an incredible long text</cell>
            <cell />
        </row>
    </table>
</text>

XSLT:
<xsl:stylesheet version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:template match="/">
      <xsl:apply-templates/>
   </xsl:template>

     <xsl:template match="table">
         <xsl:variable name="longest1">
         <xsl:sequence select=
            "/*/table/row/cell[1][not(string-length(.) &lt; /*/table/row/cell[1]/string-length(.))]"/>
         </xsl:variable>
<xsl:value-of select="longest1">
</xsl:template>
</xsl:stylesheet>

The output of course should be "first cell second row", as the second column is not processed.
I'm quite sure all I have to do is to fix the /* of this line:
<xsl:sequence select=
                "/*/table/row/cell[1][not(string-length(.) &lt; /*/table/row/cell[1]/string-length(.))]"/>

But I can't manage to see the solution.

A:

As you are writing a template for the table you can simply use
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="table">
        <xsl:sequence select="row/cell[1][not(string-length() &lt; current()/row/cell[1]/string-length())]"/>
    </xsl:template>

</xsl:stylesheet>

Of course another approach would simply be to sort row/cell[1] by its string-length and taking the last, done in XSLT 3.0 with XPath 3.1 using sort(row/cell[1], function($c) { string-length($c)})[last()] or in XSLT 2.0 using <xsl:variable name="sorted-cells" as="element(cell)*"><xsl:perform-sort select="row/cell[1]"><xsl:sort select="string-length()"/></xsl:perform-sort></xsl:variable><xsl:copy-of select="$sorted-cells[last()]"/>.

