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

Count a specific word in a document

Ronald Bourret rpbourret at rpbourret.com
Tue Jun 12 22:41:02 PDT 2007


  Count a specific word in a document
Try:

  for $elijah in doc("/db/mjs/ElijahLibretto.xhtml")/html
  let $elijah-para := $elijah//td/p[i/text() = 'Elijah']
  let $txt := string-join($elijah-para/text(), " ")
  let $words := tokenize($txt,"(\s|[,.!:;]|[n][b][s][p][;])+")
  let $lord_tokens := (for $word in $words
                       where $word = 'Lord'
                       return $word)
  return fn:count($lord_tokens)

I assume the following would also work:

  let $lord_tokens := $words[. = 'Lord']

-- Ron

Michael Strasser wrote:

> I am learning XQuery and have set myself a little task that currently I 
> can't manage.
> 
> I have an XHTML document with the complete text of Mendelssohn's 
> oratorio "Elijah" and wanted to use XQuery to count the number of times 
> the character of Elijah sings the word "Lord". I was inspired by 
> Jonathan Robie's blog post last year about word counts of DocBook 
> documents. (I copied his tokenize() example without fully understanding 
> it yet.)
> 
> I have isolated Elijah's speeches and converted the words to a sequence 
> of string tokens:
> 
>  for $elijah in doc("/db/mjs/ElijahLibretto.xhtml")/html
>  let $elijah-para := $elijah//td/p[i/text() = 'Elijah']
>  let $txt := string-join($elijah-para/text(), " ")
>  let $words := tokenize($txt,"(\s|[,.!:;]|[n][b][s][p][;])+")
> 
> I can't figure out how to count the number of string tokens that are 
> 'Lord'. I can get them with:
> 
>  for $word in $words
>  return $word[$word = 'Lord']
> 
> but I can't seem to get the count of them.
> 
> Thanks in advance for any help.
> 
> 
> Michael Strasser
> Brisbane Australia



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-2007 All Rights Reserved.