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.
db2ts "ENABLE DATABASE FOR TEXT"
db2ts "CREATE INDEX prod_desc_idx FOR TEXT ON product(description)"
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"
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.
xquery declare default element namespace "http://posample.org";
db2-fn:xmlcolumn-contains('PRODUCT.DESCRIPTION', '@xmlxp:
''/product/description/details [. contains ("ergonomic")]''')/product/..
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/..
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