xsl-list
[Top] [All Lists]

RE: Max count?

2005-07-19 03:57:44

I have some xml which looks like :-

<datanode>
      <column id="1">
              <line>text</line>
              <line>text</line>
      </column>
      <column id="2">
              <line>text</line>
              <line>text</line>
              <line>text</line>
      </column>
      <column id="3">
              <line>text</line>
      </column>
</datanode>

I need to retrieve the maximum number of line nodes contained 
by a column node within the datanode node. So for the example 
xml it would be 3, as column 2 has three line nodes.

Can anyone give me any pointers?

In XSLT 1.0 I think the solution for finding the min/max of anything is
to sort the elements and then pick the first, eg:

<xsl:template match="/">
        <xsl:for-each select="datanode/column">
                <xsl:sort select="count(line)" order="descending"/>
                <xsl:if test="position() = 1">
                        <xsl:value-of select="count(line)"/>
                </xsl:if>
        </xsl:for-each>
</xsl:template>

In XSLT 2.0 you have the max() function, so you can write:

<xsl:value-of select="max((for $i in //column return count($i/line)))"/>

cheers
andrew

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>