Db2 Text Search XML namespaces

Searching on XML namespaces requires a workaround.

You can index XML documents that contain namespace bindings without generating errors, but the namespace information is removed from each tag. As a result, text searches on XML documents with namespace bindings can lead to undesired results.

However, there is a workaround to this limitation for queries that use Db2 XQuery. The Db2 Text Search engine is not namespace aware, but you can use the Db2 XQuery support for namespaces to do namespace filtering for the unwanted documents returned from a text search.

Consider the following example in which the default database environment variable is set to SAMPLE and a text search index called prod_desc_idx is created on the PRODUCT table:
	db2ts "ENABLE DATABASE FOR TEXT" 
	db2ts "CREATE INDEX prod_desc_idx FOR TEXT ON product(description)"
Now, a new row with the namespace http://posample.org/wheelshovel is added to the PRODUCT table, which already has two XML documents with the namespace http://posample.org:
	INSERT INTO PRODUCT VALUES ('100-104-01', 'Wheeled Snow Shovel', 
	99.99, NULL, NULL, NULL, XMLPARSE(DOCUMENT '<product xmlns=
	"http://posample.org/wheelshovel" pid="100-104-01">
	<description><name>Wheeled Snow Shovel</name><details>
	Wheeled Snow Shovel, lever assisted, ergonomic foam grips, 
	gravel wheel, clears away snow 3 times faster</details>
	<price>99.99</price></description></product>'))
The text search index is then updated, as follows:
	db2ts "UPDATE INDEX prod_desc_idx FOR TEXT"
The following XQuery expression, which specifies the default element as http://posample.org, returns all documents that have the matching XPath /product/description/details that contains the word ergonomic:
	xquery declare default element namespace "http://posample.org"; 
	db2-fn:xmlcolumn-contains('PRODUCT.DESCRIPTION', '@xmlxp:
	''/product/description/details [. contains ("ergonomic")]''')

Three documents are returned, two of which are expected because they have the namespace http://posample.org and one of which is unexpected because it has the namespace http://posample.org/wheelshovel.

The following XQuery expression uses the path expression /product/.. to use the Db2 XQuery support for XML search and namespaces to filter the documents returned by Db2 Text Search engine so that only documents with the namespace http://posample.org are returned:
	xquery declare default element namespace "http://posample.org"; 
	db2-fn:xmlcolumn-contains('PRODUCT.DESCRIPTION', '@xmlxp:
	''/product/description/details [. contains ("ergonomic")]''')/product/..
Note: SQL queries can use Db2 XQuery to force namespace filtering. Given the previous example, the corresponding expression using an SQL query is as follows:
	xquery declare default element namespace "http://posample.org"; 
	db2-fn:sqlquery("select description from product where 
	contains(description, '@xmlxp:''/product/description/details 
	[. contains (""ergonomic"")]''') = 1")
The workaround is as follows:
	xquery declare default element namespace "http://posample.org"; 
	db2-fn:sqlquery("select description from product where 
	contains(description, '@xmlxp:''/product/description/details 
	[. contains (""ergonomic"")]''') = 1")/product/..
Similarly, to access a specific element in the document (as opposed to just having the matching document returned, as in the previous query), the following query can be used:
	xquery declare default element namespace "http://posample.org"; 
	db2-fn:xmlcolumn-contains('PRODUCT.DESCRIPTION', '@xmlxp:
	''/product/description/details [. contains ("ergonomic")]''')
	/product/description[price > 20]/name
Note: This workaround is limited and might not work as expected if, for example, multiple product elements exist within a document.