[xquery-talk] Count a specific word in a document

Bas de Bakker bas at x-hive.com
Thu Jun 14 09:53:40 PDT 2007

Michael Kay wrote:

> In fact the use of /text() is very common in XQuery circles, and in my view
> it's usually wrong. You nearly always want the string value of the element
> rather than its text node children: /string() rather than /text(), except as
> I say that it's usually implicit.

Indeed, which allows me to return to one of my pet peeves: bad examples 
in the XQuery Use Cases document, which many people use as examples. In 
this case, we read: Q9

In the document "books.xml", find all section or chapter titles that 
contain the word "XML", regardless of the level of nesting.

Solution in XQuery:

     for $t in doc("books.xml")//(chapter | section)/title
     where contains($t/text(), "XML")
     return $t

Bas de Bakker

More information about the talk mailing list