[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

XQuery simulation of XSLT 2.0 grouping

David Sewell dsewell at virginia.edu
Sat Oct 22 13:36:07 PDT 2005


xslt 2.0 grouping
Early in his XSLT 2.0 Programmer's Reference, Michael Kay presents an
example of the power of XSLT 2.0 by giving the brief code required to
produce a word frequency list, sorted in descending order of frequency,
for all the words in a document (using Shakespeare's "Othello" in XML as
an example). This is the template that does the work:

  <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>

The following XQuery produces the identical output:

  declare variable $corpus :=
      for $w in tokenize(doc("othello.xml"), '\W+') return lower-case($w);
  declare variable $wordList := distinct-values($corpus);
  <wordcount> {
       for $w in $wordList
       let $freq := count($corpus[. eq $w])
       order by $freq descending
       return <word word="{$w}" frequency="{$freq}"/>
  }</wordcount>

However, on my system the XSLT version takes 1.93 seconds to execute
using Saxon 8.51, while the XQuery takes 210 seconds. I realize that
XQuery 1.0 does not contain the grouping facilities of XSLT 2.0, but
I still have a couple of questions:

1. Am I overlooking a more efficient way of writing the query?

2. If not, is the assumption that one will need to rely on
   implementation-dependent optimization for this type of
   XQuery code, possibly relying on extension functions?

David

-- 
David Sewell, Editorial and Technical Manager
Electronic Imprint, The University of Virginia Press
PO Box 400318, Charlottesville, VA 22904-4318 USA
Courier: 310 Old Ivy Way, Suite 302, Charlottesville VA 22903
Email: http://xquery.com/mailman/listinfo/talk   Tel: +1 434 924 9973
Web: http://www.ei.virginia.edu/


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.