2

I'm trying to make a table with counts grouped by labels. I would like to count how many times each levels of "standings_level" are in my file.

Here is an excerpt of my XML file :

<entries>
  <entry>
    <standings_levels>
      <standings_level>4</standings_level>
    </standings_levels>
  </entry>
  <entry>
    <standings_levels>
      <standings_level>3</standings_level>
    </standings_levels>
  </entry>
  ....
 </entries>

I tried with for-each-group in xsl 2.0 :

<xsl:for-each-group select="entries/entry/standings_levels/standings_level" group-by=".">
    <tr>
        <td>
            <xsl:value-of select="current-grouping-key()"/>
        </td>
        <td>
            <xsl:value-of select="count(current-group())"/>
        </td>
    </tr>
</xsl:for-each-group>

But it doesn't return anything, what am I doing wrong ?

Here is my full XSL file.

<?xml version="1.0"?>

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

    <xsl:template match="/">
        <html lang="en">
            <xsl:call-template name="header" />
            <body>
                <xsl:call-template name="barreMenu" />
                <div id="wrap">
                    <div class="container">

                        <table class="table table-bordered table-hover table-striped" id="tableTest">
                            <thead>
                                <tr>
                                    <th>Label</th>
                                    <th>Count</th>
                                </tr>
                            </thead>
                            <tbody>
                                <xsl:for-each-group select="entries/entry/standings_levels/standings_level" group-by=".">
                                    <tr>
                                        <td>
                                            <xsl:value-of select="current-grouping-key()"/>
                                        </td>
                                        <td>
                                            <xsl:value-of select="count(current-group())"/>
                                        </td>
                                    </tr>
                                </xsl:for-each-group>
                            </tbody>
                        </table>

                    </div><!-- /.container -->
                </div>
                <xsl:call-template name="footer" />

            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Thanks for any help.

Julien Navarre
  • 7,653
  • 3
  • 42
  • 69

1 Answers1

4

Post minimal but complete code samples allowing us to reproduce the problem. When I use Saxon 9.5 with the stylesheet

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

    <!--<xsl:include href="../include/template.xsl" />-->
<xsl:output indent="yes"/>
    <xsl:template match="/">
        <html lang="en">
            <!--<xsl:call-template name="header" />-->
            <body>
                <!--<xsl:call-template name="barreMenu" />-->
                <div id="wrap">
                    <div class="container">

                        <table class="table table-bordered table-hover table-striped" id="tableTest">
                            <thead>
                                <tr>
                                    <th>Label</th>
                                    <th>Count</th>
                                </tr>
                            </thead>
                            <tbody>
                                <xsl:for-each-group select="entries/entry/standings_levels/standings_level" group-by=".">
                                    <tr>
                                        <td>
                                            <xsl:value-of select="current-grouping-key()"/>
                                        </td>
                                        <td>
                                            <xsl:value-of select="count(current-group())"/>
                                        </td>
                                    </tr>
                                </xsl:for-each-group>
                            </tbody>
                        </table>

                    </div><!-- /.container -->
                </div>
                <!--<xsl:call-template name="footer" />-->

            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

and the input

<entries>
  <entry>
    <standings_levels>
      <standings_level>4</standings_level>
    </standings_levels>
  </entry>
  <entry>
    <standings_levels>
      <standings_level>3</standings_level>
    </standings_levels>
  </entry>
  ....
 </entries>

I get the result

<html lang="en">
   <body>
      <div id="wrap">
         <div class="container">
            <table class="table table-bordered table-hover table-striped" id="tableTest">
               <thead>
                  <tr>
                     <th>Label</th>
                     <th>Count</th>
                  </tr>
               </thead>
               <tbody>
                  <tr>
                     <td>4</td>
                     <td>1</td>
                  </tr>
                  <tr>
                     <td>3</td>
                     <td>1</td>
                  </tr>
               </tbody>
            </table>
         </div>
      </div>
   </body>
</html>

So the problem is caused by different input (watch out for xmlns="...") or different XSLT code, not by the one you posted.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110