Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL xpath concatenation operator, how to add space?

Tags:

mysql

xpath

This does what I would expect:

mysql> select ExtractValue("<x><a>1</a><b>2</b></x>", "concat(/x/a, /x/b)");
+---------------------------------------------------------------+
| ExtractValue("<x><a>1</a><b>2</b></x>", "concat(/x/a, /x/b)") |
+---------------------------------------------------------------+
| 12                                                            |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

But why does this return "1" instead of "1 2"?

mysql> select ExtractValue("<x><a>1</a><b>2</b></x>", "concat(/x/a, ' ', /x/b)");
+--------------------------------------------------------------------+
| ExtractValue("<x><a>1</a><b>2</b></x>", "concat(/x/a, ' ', /x/b)") |
+--------------------------------------------------------------------+
| 1                                                                  |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)
like image 635
Alex R Avatar asked Mar 17 '23 03:03

Alex R


1 Answers

This is a verified bug. MySQL's xpath concatenation operator only considers the first 2 parameters when it should consider all parameters.

https://bugs.mysql.com/bug.php?id=71704

As a workaround you can select the individual values and pass them to the regular concat function:

select concat(ExtractValue("<x><a>1</a><b>2</b></x>", "/x/a"),' ',
  ExtractValue("<x><a>1</a><b>2</b></x>", "/x/b"));
like image 189
FuzzyTree Avatar answered Mar 19 '23 09:03

FuzzyTree