I am looping through groups of items and getting the item count from each. All I need is to get a number, the number I need is the highest item count from each items group. I thought if I could create an array from the counts that are coming back I could then pluck the first one from the array and utilize that number to go further with my project.
Here is my xml
<example>
<items>
<item>1</item>
<item>2</item>
<item>3</item>
</items>
<items>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</items>
<items>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</items>
<items>
<item>1</item>
<item>2</item>
</items>
Here is my xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="example/items">
<xsl:sort select="count(item)" data-type="number" order="descending"/>
<xsl:value-of select="count(item)"/>,
</xsl:for-each>
</xsl:template>
As of now this outputs in the browser 5, 4, 3, 2, I was able to get the highest count do be first using sort, but these are all separate values getting printed to the screen after each iteration.
How can I get them into something usable like (5, 4, 3, 2,)?
Or somehow just get the number 5 returned to me.
Any help would be greatly appreciated.
To output the highest which is the first in your descending sorting order use
<xsl:template match="/">
<xsl:for-each select="example/items">
<xsl:sort select="count(item)" data-type="number" order="descending"/>
<xsl:if test="position() = 1"><xsl:value-of select="count(item)"/></xsl:if>
</xsl:for-each>
</xsl:template>
If you wanted to return some "complex" structure with all the counted item numbers then you would need to look into XSLT 2.0 where you then have the choice of returns a sequence of number values or a sequence of XML element nodes storing the numbers. In XSLT 1.0 you could build a result tree fragment of XML element nodes storing the numbers but to access them then with XPath you would need exsl:node-set
or similar.
But in XSLT 2.0 there are direct ways like max(example/items/count(item))
to compute the maximum item count.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.