63590

sum of count in xslt-3

Question:

Given the following example,

<a href="https://xsltfiddle.liberty-development.net/3NzcBsV" rel="nofollow">https://xsltfiddle.liberty-development.net/3NzcBsV</a>

each description must occur only once. How one could add in the first row of the output report the number of description elements that have occurred more than once in the xml file under question?

Desired output:

3 (because "Light Belgian waffles covered with an assortment of fresh berries and whipped cream" was found 2 times, and "Two of our famous Belgian Waffles with plenty of real maple [syrup]" was found 3 times, thus 1+2=3 ) 02 Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 04 Light Belgian waffles covered with an assortment of fresh berries and whipped cream 100 01 Two of our famous Belgian Waffles with plenty of real maple [syrup] 650 01 Two of our famous Belgian Waffles with plenty of real maple [syrup] 350 05 Two of our famous Belgian Waffles with plenty of real maple [syrup] 250

Answer1:

Store the grouped elements into a container element and count the items of the different groups (or substract one from that count as you seem to want):

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"> <xsl:output method="text" /> <xsl:template match="breakfast_menu"> <xsl:variable name="groups"> <xsl:for-each-group select="food" group-by="description"> <xsl:sort select="current-grouping-key()"/> <xsl:if test="current-group()[2]"> <group key="{current-grouping-key()}"> <xsl:copy-of select="current-group()"/> </group> </xsl:if> </xsl:for-each-group> </xsl:variable> <xsl:value-of select="sum($groups/group!(count(food) - 1))"/> <xsl:text>&#10;</xsl:text> <xsl:apply-templates select="$groups/group/food"/> </xsl:template> <xsl:template match="food"> <xsl:value-of select="name, description, string(calories)" separator="&#9;"/> <xsl:text>&#10;</xsl:text> </xsl:template> </xsl:stylesheet>

<a href="https://xsltfiddle.liberty-development.net/3NzcBsV/4" rel="nofollow">https://xsltfiddle.liberty-development.net/3NzcBsV/4</a>

Recommend

  • NSString alloc/init generates 'Redefinition of myStr' error…?
  • Select results from multiple array elements in Json document with Sql Server 2016's OPENJSON fu
  • Counting word occurrences in csv and determine row appearances
  • Recommended way to insert elements into map [duplicate]
  • Limit SQL query to only the top two counts per group [duplicate]
  • How do I use C# to encrypt another program?
  • PHP function iconv character encoding from iso-8859-1 to utf-8
  • Hive query generating identifiers for a sequence of row matching a condition
  • Is there a Way to take .cur file formatted Cursor as the Cursor of a Java Application?
  • Why does calling TreeViewer.refresh() not call the contentProvider's getChildren(Object) method
  • error with magento checkout
  • Unable to render excel download from aspx page
  • Refreshing the Items in an XMLDataSource bound ASP.NET ListView
  • ToolStrip LineStyles .Net
  • Find and append hrefs of a certain class
  • Does WEBSQL support Phonegap Apps on Ice Cream Sandwich?
  • Accessing ArrayList elements
  • How to check a single time if UserDefaults is empty
  • Running dpm with Runtime.exec(…)
  • Primefaces nested p:dialog DOM node duplicates
  • Incrementing value and setting it as var to onClick event
  • The JavaScript language service has encountered an error and has been shut down
  • iPhone app startup
  • How can I prevent IntelliJ IDEA from reassigning file type associations at startup?
  • Can't start a new project because of gradle error
  • How do I use Java generic wildcards with methods taking more than one generic parameter?
  • AWS java SDK manually set signature version
  • My PHP code is commented out
  • C++ boost::spirit parsing embedded languages
  • Ncurses No Output
  • Group variable in cobol
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Merge Module leaving files during uninstall
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • Atlas images wrong size on iPad iOS 9
  • NetLogo BehaviorSpace - Measure runs using reporters
  • ActionScript 2 vs ActionScript 3 performance
  • WOWZA + RTMP + HTML5 Playback?
  • Error creating VM instance in Google Compute Engine
  • java string with new operator and a literal