简体   繁体   中英

MYSQL EXTRACTVALUE with “<” Less Than Operator returns NULL?

I am using MYSQL with the EXTRACTVALUE command, and the below code returns NULL when using the "<" Less Than Operator. But the ">" Greater Than Operator works as intended. Why?

select EXTRACTVALUE('<TrackerQuery>
  <Column>*</Column>
  <View> view_sms_speqs</View>
  <Filters>
    <Filter>
      <FilterColumn>DateTested</FilterColumn>
      <FilterOperator><</FilterOperator>
      <FilterValue>2019-04-18</FilterValue>
    </Filter>
  </Filters>
</TrackerQuery>', CONCAT('//TrackerQuery//Filters//Filter[', 1, ']//FilterOperator'));

The problem is that the content <tag><</tag> is not valid, resulting in NULL being returned. This is mentioned in the documentation for EXTRACTVALUE :

NULL is returned if xml_frag contains elements which are not properly nested or closed, and a warning is generated, as shown in this example: [...]

You will see in the following example that there was an error/warning when you use < as a content because it looks like it is the start of a new XML element.

mysql> SELECT EXTRACTVALUE('<abc><def><</def></abc>', '//abc//def');
+-------------------------------------------------------+
| EXTRACTVALUE('<abc><def><</def></abc>', '//abc//def') |
+-------------------------------------------------------+
| NULL                                                  |
+-------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> SHOW WARNINGS;
+---------+------+-------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                   |
+---------+------+-------------------------------------------------------------------------------------------+
| Warning | 1525 | Incorrect XML value: 'parse error at line 1 pos 13: '<' unexpected (ident or '/' wanted)' |
+---------+------+-------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Interesting enough, the value &lt; is not working as it should.

mysql> SELECT EXTRACTVALUE('<abc><def>&lt;</def></abc>', '//abc//def');
+----------------------------------------------------------+
| EXTRACTVALUE('<abc><def>&lt;</def></abc>', '//abc//def') |
+----------------------------------------------------------+
| &lt;                                                     |
+----------------------------------------------------------+
1 row in set (0.00 sec)

But when you put it in a CDATA block, you will get the correct value.

mysql> SELECT EXTRACTVALUE('<abc><def><![CDATA[<]]></def></abc>', '//abc//def');
+-------------------------------------------------------------------+
| EXTRACTVALUE('<abc><def><![CDATA[<]]></def></abc>', '//abc//def') |
+-------------------------------------------------------------------+
| <                                                                 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM