Use count() and current-group() to count groups : current group « XSLT stylesheet « XML






Use count() and current-group() to count groups


File: Data.xml

<?xml version="1.0" encoding="UTF-8"?>
<employees>
  <employee>
    <FirstName>A</FirstName>
    <LastName>B</LastName>
    <Country>USA</Country>
  </employee>
  <employee>
    <FirstName>C</FirstName>
    <LastName>D</LastName>
    <Country>USA</Country>
  </employee>
  <employee>
    <FirstName>E</FirstName>
    <LastName>F</LastName>
    <Country>USA</Country>
  </employee>
</employees>


File: Transform.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <h3>header</h3>
        <xsl:for-each-group select="employees/employee"
          group-by="Country">
          <xsl:sort select="current-grouping-key()" />
          <p>
            Number of employees who live in:
            <b>
              <xsl:value-of
                select="current-grouping-key()" />
            </b>
            is
            <xsl:value-of select="count(current-group())" />
          </p>
          <xsl:result-document
            href="{current-grouping-key()}.xml">
            <employees>
              <xsl:copy-of select="current-group()" />
            </employees>
          </xsl:result-document>
        </xsl:for-each-group>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

 








Related examples in the same category

1.Get current group
2.Word count