RE: [xsl] Max count?

Subject: RE: [xsl] Max count?
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Tue, 19 Jul 2005 11:57:44 +0100
> 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

Current Thread