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

I search through an XML file with this query:


declare namespace tf="http://namespaces.softwareag.com/tamino/TaminoFunction"
declare namespace ino = "http://namespaces.softwareag.com/tamino/response2"
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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
	<Title DocID="1234">test</Title>
	<DocInfo CommentsFlag="true" Continued="true" References="true">
		<Template>
			<TempName>String</TempName>
			<TempVersion>0.0</TempVersion>
		</Template>
		<Authors>
			<Author>
				<AuthorName>String</AuthorName>
				<AuthorDept>String</AuthorDept>
			</Author>
		</Authors>
		<Abbreviations>
			<Abbreviation>
				<A_Term>String</A_Term>
				<A_Definition>String</A_Definition>
			</Abbreviation>
		</Abbreviations>
		<References>
			<RefReferences>
				<RefID>String</RefID>
				<RefTitle>String</RefTitle>
			</RefReferences>
		</References>
		<Product>test hallo</Product>
	</DocInfo>
</UseCaseSpec>

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.

Hi,

please use the following query:


declare namespace tf="http://namespaces.softwareag.com/tamino/TaminoFunction"
declare namespace ino = "http://namespaces.softwareag.com/tamino/response2"
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

Harald

Thank you for your fast help. It works fine.