Xquery with text-contains

Hello,

I’m about to learn using Xquery with Tamino. Everything was going well so far but now I got stuck (although trying to get a solution for several hours…) Hope you can help me out, please.

I have XML-data containing some newspaper articles which has some text-elements. Now, I want to find the article(s) whose text-element contains the word “politics” and get as a result the text and the id-attribute of its article-element.

<issue>
 <article id="1">
   <text>xxx xxx politics xxx xxx xxx</text>
 </article> 
 <article id="2">
  <text>xxx xxx xxx xxx xxx xxx</text>
 </article>
</issue>

What I already have is an Xquery that gets all the text-elements which contain “politics” but not the id-attributes of their article-elements. How do I have to extend my xquery? Or is it a totally wrong way?

declare namespace ft="http://www.w3.org/2002/04/xquery-operators-text"
for $q in input()/issue/article//text
where ft:text-contains($q, "politics")
return $q

Thanks a lot in advance!

Hi,

first of all: please do not use ft: functions, they are deprecated. Please use functions from the tf: namespace, in this case tf:containsText.

Concerning your question:

I changed the namespace, but in Tamino 4.4. this namespace is pre-declared, so you can also leave out the “declare namespace” line.

Your query is almost what you want to do, just iterate on the article level in acddition to the text node level, please see below:


declare namespace tf="http://namespaces.softwareag.com/tamino/TaminoFunction"

for $article in input()/issue/article
for $text in $article//text
where tf:containsText($text, "politics")
return <Textfragment>{$article/@id,$text}</Textfragment>

Regards

Harald

Harald, thanks a lot for your answer. It works fine! I am using Tamino 4.2, so the ft:functions worked well but thanks for the hint.

Is the xpath following/preceding-axis already implemented in version 4.2? Imagine above xml-document having more text-elements inside an article-element and i want not only to get the text-element with the searched keyword but also one text-element before and one behind it to have some context. Can I use preceding/following-axis or is there another way to do it? How do I have to extend the last xquery.

Thanks in advance!

Max

From the Tamino XQuery reference manual (xqueryref/ref-Axis.htm), the only axis specifications supported are child, descendant, parent, ancestor, attribute, self, and descendant-or-self.