Problem to get correct search results with tf:containsText()

I search through an XML file with this query:

declare namespace tf=""
declare namespace ino = ""
for $q in input()/test
where $q//*[tf:containsText(., "test")]
return <result>{$q/Title}<ino:id>{tf:getInoId($q)}</ino:id><searchresult>{$q//*[tf:containsText(., "test")]}</searchresult></result>

As result I expect only the elements, which the searched string “test” contain.

But as results I get all nodes and elements from the root to the next upper node of the element.

What’s wrong with the query?

Example XML-file:

<?xml version="1.0" encoding="UTF-8"?>
<UseCaseSpec xmlns:xsi="" xsi:noNamespaceSchemaLocation="test.xsd">
	<Title DocID="1234">test</Title>
	<DocInfo CommentsFlag="true" Continued="true" References="true">
		<Product>test hallo</Product>

If I search the string “test” I get “test hallo” as one result an the howle node “DocInfo” with all lower nodes another result.

Your example XML does not match the query: the root of the example is “UseCaseSpec”, not “test”.

Maybe you could post the XSD along with the sample XML that matches your query.


please use the following query:

declare namespace tf=""
declare namespace ino = ""
for $q in input()/UseCaseSpec
where $q//*[tf:containsText(., "test")]
return <result>{$q/Title}<ino:id>{tf:getInoId($q)}</ino:id><searchresult>{$q//*[tf:containsText(./text(), "test")]}</searchresult></result> 

Note that tf:contains works on the textual value of an element, which is constructed by recursively concatenating all text children of the element. As a consequence, if an element contains the text searched, all elements containing this element contain the text as well


Thank you for your fast help. It works fine.