错误处理
对ExtractValue() 和 UpdateXML(),使用的XPath定位器必须是有效的,被搜索的XML必须是结构良好的,如果定位器无效,则会产生一个错误。
mysql> SELECT @new_xml_node:=UpdateXML('<state><city/></state>',
-> '//city/"state', '<county><city/></county>') AS xml_node;
ERROR 1105 (HY000): XPATH syntax error: '"state'
-> '//city/"state', '<county><city/></county>') AS xml_node;
ERROR 1105 (HY000): XPATH syntax error: '"state'
如果被搜索的XML结构不好,则会返回null,并产生一个警告。
mysql> SELECT @new_xml_node:=UpdateXML('<state><city></state>', '//city',
-> '<county><city/></county>') AS xml_node;
+----------+
| xml_node |
+----------+
| NULL |
+----------+
1 row in set, 1 warning (0.01 sec)
-> '<county><city/></county>') AS xml_node;
+----------+
| xml_node |
+----------+
| NULL |
+----------+
1 row in set, 1 warning (0.01 sec)
可以使用show warnings命令显示警告。
mysql> show warnings;
+---------+------+---------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------+
| Warning | 1525 | Incorrect XML value: 'parse error at line 1 pos 21: '</state>' unexpected ('</city>' wanted)' |
+---------+------+---------------------------------------------------------------+
1 row in set (0.01 sec)
+---------+------+---------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------+
| Warning | 1525 | Incorrect XML value: 'parse error at line 1 pos 21: '</state>' unexpected ('</city>' wanted)' |
+---------+------+---------------------------------------------------------------+
1 row in set (0.01 sec)
作为第三个参数传递给UpdateXML()的替换XML则不会检查结构。
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.00 sec)
-> '<<county><city/></county>>') AS xml_node;
+-------------------------------------------+
| xml_node |
+-------------------------------------------+
| <state><<county><city/></county>></state> |
+-------------------------------------------+
1 row in set (0.00 sec)
在XPath表达式中使用变量
从MySQL 5.1.20开始,可以在XPath定位器参数中使用变量,这样在传递参数时就更加灵活了,根据使用的语法不同,可以对变量实施弱检查或强检查。