How to use XPATH in MySQL select?

2020-03-03 07:07发布

Say I have a table called "xml" that stores XML files in a single column "data". How would I write a MySQL query that run an XPath and return only rows matching that XPath?

标签: sql mysql xpath
2条回答
我命由我不由天
2楼-- · 2020-03-03 07:32

I just got the answer from a colleague, it seems trimming the xml often helps:

select * from xml where 
   trim(both '\r\n' from ExtractValue(xml, '/some/xpath')) = 'value';
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-03-03 07:37
SELECT * FROM xml
WHERE EXTRACTVALUE(data, '<xpath-expr>') != '';

You should note, however, that there are limitations to MySQL's support of XPath.

  • EXTRACTVALUE() returns only CDATA.
  • Not all XPath constructions are supported. Details under the heading "XPath limitations" on the doc page mentioned in abatishchev's answer.
查看更多
登录 后发表回答