我试图使用ExtractValue MYSQL函数从存储在我的数据库的一列中的xml返回一个段.以下是我设置所有内容的方法.
表:
create table documents
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
application_id int NOT NULL,
content MEDIUMTEXT NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
插入:
insert into documents values (null, 1,
'<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>');
Run Code Online (Sandbox Code Playgroud)
查询:
SELECT content from documents into @xml;
SELECT ExtractValue(@xml, '/bookstore');
Run Code Online (Sandbox Code Playgroud)
第二个查询返回一个非常大的空文本空间.几乎看起来空的空间等于应该返回的空间并且有趣的是当我使用应该返回较小结果的xpath时,空白字段会缩小.
我非常感谢这个问题的一些帮助,并乐意提供更多的信息或尝试任何东西.
这不是ExtractValue的用途.它用于提取特定值,而不是整个元素.
下面是使用ExtractValue和示例XML字符串来提取价格值的示例:
mysql> set @xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
'> <bookstore>
'> <book>
'> <title lang="eng">Harry Potter</title>
'> <price>29.99</price>
'> </book>
'> <book>
'> <title lang="eng">Learning XML</title>
'> <price>39.95</price>
'> </book>
'> </bookstore>';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> SELECT ExtractValue(@xml, '/bookstore/book/price');
+---------------------------------------------+
| ExtractValue(@xml, '/bookstore/book/price') |
+---------------------------------------------+
| 29.99 39.95 |
+---------------------------------------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6417 次 |
| 最近记录: |