ExtractValue(xml_frag
, xpath_expr
)
ExtractValue()
takes two string arguments, a fragment of XML markup xml_frag
and an XPath expression xpath_expr
(also known as a locator); it returns the text (CDATA
) of the first text node which is a child of the element(s) matched by the XPath expression. It is the equivalent of performing a match using the xpath_expr
after appending /text()
. In other words, ExtractValue('<a><b>Sakila</b></a>', '/a/b')
and ExtractValue('<a><b>Sakila</b></a>', '/a/b/text()')
produce the same result.
//入口参数:xml_frag,就是一个xml文档。
//xpath_expr,是要查找的路径表达式。加不加/text(),返回都是一样的。
//返回:元素的值
If multiple matches are found, then the content of the first child text node of each matching element is returned (in the order matched) as a single, space-delimited string.
//如果有多个元素被找到,返回所有匹配的元素值,以匹配的顺序出现,用空格分开。
If no matching text node is found for the (augmented) expression — for whatever reason, as long as xpth_expr
is valid, and xml_frag
is well-formed — an empty string is returned. No distinction is made between a match on an empty element and no match at all. This is by design.
//如果没有找到,返回空。或者找到了,值为空,也返回空。
If you need to determine whether no matching element was found in xml_frag
or such an element was found but contained no child text nodes, you should test the result of an expression that uses the XPath count()
function. For example, both of these statements return an empty string, as shown here:
mysql> SELECT ExtractValue('<a><b/></a>', '/a/b');
+-------------------------------------+
| ExtractValue('>a<>b/<>/a<', '/a/b') |
+-------------------------------------+
| |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT ExtractValue('<a><c/></a>', '/a/b');
+-------------------------------------+
| ExtractValue('<a><c/></a>', '/a/b') |
+-------------------------------------+
| |
+-------------------------------------+
1 row in set (0.00 sec)
However, you can determine whether there was actually a matching element using the following:
mysql> SELECT ExtractValue('<a><b/></a>', 'count(/a/b)');
+-------------------------------------+
| ExtractValue('<a><b/></a>', 'count(/a/b)') |
+-------------------------------------+
| 1 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT ExtractValue('<a><c/></a>', 'count(/a/b)');
+-------------------------------------+
| ExtractValue('<a><c/></a>', 'count(/a/b)') |
+-------------------------------------+
| 0 |
+-------------------------------------+
1 row in set (0.01 sec)
//如果你要区别上述情况,请用count()函数。
Note that ExtractValue()
returns only CDATA
, and does not return any tags that might be contained within a matching tag, nor any of their content (see the result returned as val1
in the following example).
//函数只返回CDATA,不返回任何标记和他们中间的内容。
mysql> SELECT
-> ExtractValue('<a>ccc<b>ddd</b></a>', '/a') AS val1,
-> ExtractValue('<a>ccc<b>ddd</b></a>', '/a/b') AS val2,
-> ExtractValue('<a>ccc<b>ddd</b></a>', '//b') AS val3,
-> ExtractValue('<a>ccc<b>ddd</b></a>', '/b') AS val4,
-> ExtractValue('<a>ccc<b>ddd</b><b>eee</b></a>', '//b') AS val5;
+------+------+------+------+---------+
| val1 | val2 | val3 | val4 | val5 |
+------+------+------+------+---------+
| ccc | ddd | ddd | | ddd eee |
+------+------+------+------+---------+
Beginning with MySQL 5.1.8, this function uses the current SQL collation for making comparisons with contains()
. (Previously, binary — that is, case-sensitive — comparison was always used.)
//从5.1.8开始,这个函数...?
UpdateXML(xml_target
, xpath_expr
, new_xml
)
This function replaces a single portion of a given fragment of XML markup xml_target
with a new XML fragment new_xml
, and then returns the changed XML. The portion of xml_target
that is replaced matches an XPath expression xpath_expr
supplied by the user. If no expression matching xpath_expr
is found, or if multiple matches are found, the function returns the original xml_target
XML fragment. All three arguments must be strings.
//这个函数用于更改xml文档的元素值。
//入口参数:xml_target, xpath_expr同上,new_xml:新的元素值 。
//如果没有找到或找到多个匹配的元素,则返回原始的xml文档。
mysql> SELECT
-> UpdateXML('<a><b>ccc</b><d></d></a>', '/a', '<e>fff</e>') AS val1,
-> UpdateXML('<a><b>ccc</b><d></d></a>', '/b', '<e>fff</e>') AS val2,
-> UpdateXML('<a><b>ccc</b><d></d></a>', '//b', '<e>fff</e>') AS val3,
-> UpdateXML('<a><b>ccc</b><d></d></a>', '/a/d', '<e>fff</e>') AS val4,
-> UpdateXML('<a><d></d><b>ccc</b><d></d></a>', '/a/d', '<e>fff</e>') AS val5
-> \G
*************************** 1. row ***************************
val1: <e>fff</e>
val2: <a><b>ccc</b><d></d></a>
val3: <a><e>fff</e><d></d></a>
val4: <a><b>ccc</b><e>fff</e></a>
val5: <a><d></d><b>ccc</b><d></d></a>