MySQL ExtractValue()
方法是否支持 XPath 'contains'?
我正在尝试返回以下 xml 中包含字符串 'edit:' 的所有属性
<Data>
<Row SomeAttribute="edit: return this" />
<Row SomeAttribute="not this" />
<Row SomeAttribute="or this" />
<Row SomeAttribute="edit: return this" />
</Data>
所以我尝试了:
select ExtractValue(xml, '/Data/Row[contains(@SomeAttribute, 'edit:')]')
但它返回关于字符串编辑周围的 ' 的查询语法错误:
我也试过
select ExtractValue(xml, '/Data/Row[contains(@SomeAttribute, "edit:")]')
它只返回一个空字符串?
答案 0 :(得分:1)
我不认为这是直接不可能的,但你可以这样做:
CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN `xml` varchar(400),IN `xpath` varchar(100), in f varchar(100))
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT DEFAULT 1;
SELECT extractvalue(xml,concat('count(',replace(xpath,'[$i]',''),')')) into j ;
DROP TABLE IF EXISTS temp;
CREATE TEMPORARY TABLE temp(i integer, x varchar(100));
WHILE i <= j DO
INSERT INTO temp SELECT i, ExtractValue(xml, `xpath`);
SET i = i+1;
END WHILE;
SELECT * FROM temp where x like f;
END
并像这样使用它:
SET @xml = '<Data>
<Row SomeAttribute="edit: return this" />
<Row SomeAttribute="not this" />
<Row SomeAttribute="or this" />
<Row SomeAttribute="edit: return this" />
</Data>';
call myproc(@xml,'//Row[$i]/@SomeAttribute','%edit:%');
+------+-------------------+
| i | x |
+------+-------------------+
| 1 | edit: return this |
| 4 | edit: return this |
+------+-------------------+
2 rows in set (0.00 sec)
您可能需要根据自己的需要进行一些更改(例如此过程的名称和此临时表的名称)
顺便说一句:结果存储在临时表temp
中。所以你也可以这样做,在存储过程之后:
select * from temp where .....