Bug #26518 XPath and variables problem
Submitted: 21 Feb 2007 9:34 Modified: 2 Jun 2007 14:08
Reporter: Alexander Barkov Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: XML functions Severity:S3 (Non-critical)
Version:5.1, 5.2-falcon OS:Any
Assigned to: Alexander Barkov CPU Architecture:Any

[21 Feb 2007 9:34] Alexander Barkov
Description:
mysql> set @i=1;select ExtractValue('<a><b>b1</b><b>b2</b></a>','/a/b[$i]');
Query OK, 0 rows affected (0.00 sec)

+------------------------------------------------------+
| ExtractValue('<a><b>b1</b><b>b2</b></a>','/a/b[$i]') |
+------------------------------------------------------+
| b1 b2                                                |
+------------------------------------------------------+
1 row in set (0.00 sec)

The above query should respect the value of the user variable "i"
and return only "b1". The manual page at
http://dev.mysql.com/doc/refman/5.1/en/xml-functions.html
says nothing that variables are not supported.

Or, if the variables are not supported, it should return
syntax error:

ERROR 1105 (HY000): XPATH syntax error: '$i]'

How to repeat:
Run the above query

Suggested fix:
Fix the function my_xpath_parse_VariableReference()
to create Item_func_get_user_var(), which was just obviosly
forgotten:

static int
my_xpath_parse_VariableReference(MY_XPATH *xpath)
{
  if (my_xpath_parse_term(xpath, MY_XPATH_LEX_DOLLAR) &&
      my_xpath_parse_term(xpath, MY_XPATH_LEX_IDENT))
  {
+    LEX_STRING name;
+    name.length= xpath->prevtok.end - xpath->prevtok.beg;
+    name.str= (char*) xpath->prevtok.beg;
+    xpath->item= new Item_func_get_user_var(name);
    return 1;
  }
  return 0;
}

After this fix, it works as expected:

mysql> set @i=1;select ExtractValue('<a><b>b1</b><b>b2</b></a>','/a/b[$i]');
Query OK, 0 rows affected (0.00 sec)

+------------------------------------------------------+
| ExtractValue('<a><b>b1</b><b>b2</b></a>','/a/b[$i]') |
+------------------------------------------------------+
| b1                                                   |
+------------------------------------------------------+
1 row in set (0.00 sec)

mysql> set @i=2;select ExtractValue('<a><b>b1</b><b>b2</b></a>','/a/b[$i]');
Query OK, 0 rows affected (0.00 sec)

+------------------------------------------------------+
| ExtractValue('<a><b>b1</b><b>b2</b></a>','/a/b[$i]') |
+------------------------------------------------------+
| b2                                                   |
+------------------------------------------------------+
1 row in set (0.00 sec)

Adding an error message will need the same amount of new code,
but it will be really a waste of time, because it would be
nice to support the entire XPath some day, and the variables
are an important part of it.
[21 Feb 2007 9:43] Sveta Smirnova
Thank you for the report.

Verified as described.
[27 Mar 2007 11:15] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/22998
[28 Mar 2007 8:31] Sergey Vojtovich
Approved.
[17 Apr 2007 9:10] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/24630
[2 May 2007 6:33] Alexander Barkov
Information for the DOC team:

The user has two options when using variables with XPath.

- weak type checking. In this case user variables ($@x syntax)
  should be used andthe user is fully responsible for any typos --
  he will never get a warning about it.

- strict checking. In this case SP-variables should be used ($x syntax).
  Here the user can rely on the MySQL to check the errors.

Please put this into the the manual when documenting this bug fix.
Thanks!
[2 May 2007 6:46] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/25884
[8 May 2007 8:36] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/26272

ChangeSet@1.2481, 2007-05-08 13:32:29+05:00, bar@mysql.com +3 -0
  Bug#26518 XPath and variables problem
  Problem: XPath variables didn't work.
  Fix: adding variables support,
  both user-defined and sp local variables are now supported by XPath.
[8 May 2007 9:07] Alexander Barkov
Pushed into 5.1.18-rpl
[1 Jun 2007 19:24] Bugs System
Pushed into 5.1.20-beta
[2 Jun 2007 14:08] Jon Stephens
Thank you for your bug report. This issue has been committed to our source repository of that product and will be incorporated into the next release.

If necessary, you can access the source repository and build the latest available version, including the bug fix. More information about accessing the source trees is available at

    http://dev.mysql.com/doc/en/installing-source.html

Documented fix in 5.1.20 changelog and XML Functions section of 5.1 Manual.