【IT168 技术文档】今天我要为大家介绍的是XPath,XPath是导航和查询XML文档的语言。我们从一个函数开始。
UpdateXML()函数
我们已经花了很多时间介绍ExtractValue()函数,但还没有介绍MySQL的其它XML函数,如UpdateXML(),因为我们先前主要将内容放在将XML文档中的数据导入到MySQL数据库中了,UpdateXML()是一个使用不同的XML标记匹配和替换XML块的函数。
ExtractValue()有两个字符串参数,一个XML标记,一个XPath表达式。
ExtractValue(xml_frag, xpath_expr)
它返回第一个匹配XPath表达式的文本节点。假设你想将“
mysql> SELECT @new_xml_node:=UpdateXML('<state><city/></state>',
-> '//city',
-> '<county><city/></county>')
-> AS xml_node;
+-----------------------------------------+
| xml_node |
+-----------------------------------------+
| <state><county><city/></county></state> |
+-----------------------------------------+
1 row in set (0.03 sec)
mysql> SELECT @new_xml_node;
+-----------------------------------------+
| @new_xml_node |
+-----------------------------------------+
| <state><county><city/></county></state> |
+-----------------------------------------+
1 row in set (0.00 sec)
-> '//city',
-> '<county><city/></county>')
-> AS xml_node;
+-----------------------------------------+
| xml_node |
+-----------------------------------------+
| <state><county><city/></county></state> |
+-----------------------------------------+
1 row in set (0.03 sec)
mysql> SELECT @new_xml_node;
+-----------------------------------------+
| @new_xml_node |
+-----------------------------------------+
| <state><county><city/></county></state> |
+-----------------------------------------+
1 row in set (0.00 sec)
如果没有发现匹配表达式的文本节点,就返回原始XML字符串。
mysql> SELECT @new_xml_node:=UpdateXML('<state><city/></state>',
-> '//dummy',
-> '<county><city/></county>')
-> AS xml_node;
+---------------------------+
| xml_node |
+---------------------------+
| <state><city/></state> |
+---------------------------+
1 row in set (0.03 sec)
-> '//dummy',
-> '<county><city/></county>')
-> AS xml_node;
+---------------------------+
| xml_node |
+---------------------------+
| <state><city/></state> |
+---------------------------+
1 row in set (0.03 sec)
如果发现有多个都匹配,会按顺序返回每个匹配的子文本节点的内容。
mysql> SELECT @new_xml_node:=UpdateXML('<state><city/></state><state><city/></state><state><city/></state>',
-> '//city,
-> '<county><city/></county>')
-> AS xml_node;
+--------------------------------------------------------------------+
| xml_node |
+--------------------------------------------------------------------+
| <state><city/></state><state><city/></state><state><city/></state> |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)
-> '//city,
-> '<county><city/></county>')
-> AS xml_node;
+--------------------------------------------------------------------+
| xml_node |
+--------------------------------------------------------------------+
| <state><city/></state><state><city/></state><state><city/></state> |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)
因为UpdateXML()函数在匹配到空元素和没有匹配之间没有区别,要区别它们可以使用XPath count()函数测试ExtractValue()的返回。
mysql> SELECT ExtractValue('<state><city/></state><state><city/></state><state><city/></state>',
-> 'count(//city)') AS xml_node;
+----------+
| xml_node |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT ExtractValue('<state><city/></state><state><city/></state><state><city/></state>',
-> 'count(//county)') AS xml_node;
+----------+
| xml_node |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
-> 'count(//city)') AS xml_node;
+----------+
| xml_node |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT ExtractValue('<state><city/></state><state><city/></state><state><city/></state>',
-> 'count(//county)') AS xml_node;
+----------+
| xml_node |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)