DB2 中的 XMLQuery

XMLQuery in DB2

我们在包含 XML 的 DB2 数据库中有一个 CLOB 列。查询似乎不喜欢嵌入的 xmlns。我将查询简化为:

values xmlquery('$rf/Producers/FIRMS/FIRM/EIN/text()' passing xmlparse(document '<Producers xmlns="http://yummy.cupcakes.com"><FIRMS><FIRM><EIN>27</EIN></FIRM>/</FIRMS></Producers>') as "rf")

如果我删除 Producers 中嵌入的 xmlns,它就可以工作。照原样,它不会 return 任何东西,但不会抛出错误。就像我说的,这简化了故障排除。实际上,该文档将来自我们数据库中的 CLOB 列。存储在 CLOB 中的所有 XML 都包含 xmlns。关于如何处理这个问题有什么建议吗?

您需要在 XMLQUERY 中声明默认命名空间。

SELECT XMLQUERY (
  'declare default element namespace "http://yummy.cupcakes.com";
   ...

参考link:https://www.ibm.com/docs/en/db2/11.1?topic=tutorial-lesson-6-querying-xml-data