Word count : current group « XSLT stylesheet « XML

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XSLT stylesheet » current group 
Word count


File: Data.xml 
<poem>
   <author>author 1</author>
   <date>1912</date>
   <title>Song</title>
  <stanza>
      <line>line 1</line>
      <line>line 2</line>
      <line>line 3</line>
      <line>line 4</line>
   </stanza>
   <stanza>
      <line>line 5</line>
      <line>line 6</line>
      <line>line 7</line>
      <line>line 8</line>
   </stanza>
   <stanza>
      <line>line 9</line>
      <line>line 10</line>
      <line>line 11</line>
      <line>line 12</line>
   </stanza>
</poem>

File: Transform.xslt

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

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <wordcount>
      <xsl:for-each-group group-by="."
        select="
          for $w in tokenize(string(.), '\W+') return lower-case($w)">
        <xsl:sort select="count(current-group())"
          order="descending" />
        <word word="{current-grouping-key()}"
          frequency="{count(current-group())}" />
      </xsl:for-each-group>
    </wordcount>
  </xsl:template>

</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<wordcount>
   <word word="line" frequency="12"/>
   <word word="" frequency="2"/>
   <word word="1" frequency="2"/>
   <word word="author" frequency="1"/>
   <word word="1912" frequency="1"/>
   <word word="song" frequency="1"/>
   <word word="2" frequency="1"/>
   <word word="3" frequency="1"/>
   <word word="4" frequency="1"/>
   <word word="5" frequency="1"/>
   <word word="6" frequency="1"/>
   <word word="7" frequency="1"/>
   <word word="8" frequency="1"/>
   <word word="9" frequency="1"/>
   <word word="10" frequency="1"/>
   <word word="11" frequency="1"/>
   <word word="12" frequency="1"/>
</wordcount>

 
Related examples in the same category
1. Get current group
2. Use count() and current-group() to count groups
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.