The XPATH_STRING function is used to parse the XML string and return the first XML node that matches the XPath expression.
:::tip This function is supported since version 3.0.6. :::
XPATH_STRING(<xml_string>, <xpath_expression>)
| Parameter | Description |
|---|---|
<xml_string> | Source string. Type: VARCHAR |
<xpath_expression> | XPath expression. Type: VARCHAR |
Returns VARCHAR type, representing the contents of the first XML node that matches the XPath expression.
Special cases:
SELECT xpath_string('<a>123</a>', '/a');
+-----------------------------------+ | xpath_string('<a>123</a>', '/a') | +-----------------------------------+ | 123 | +-----------------------------------+
SELECT xpath_string('<a><b>123</b></a>', '/a/b');
+--------------------------------------------+ | xpath_string('<a><b>123</b></a>', '/a/b') | +--------------------------------------------+ | 123 | +--------------------------------------------+
SELECT xpath_string('<a><b id="1">123</b></a>', '//b[@id="1"]');
+----------------------------------------------------------+ | xpath_string('<a><b id="1">123</b></a>', '//b[@id="1"]') | +----------------------------------------------------------+ | 123 | +----------------------------------------------------------+
SELECT xpath_string('<a><b>1</b><b>2</b></a>', '/a/b[2]');
+----------------------------------------------------+ | xpath_string('<a><b>1</b><b>2</b></a>', '/a/b[2]') | +----------------------------------------------------+ | 2 | +----------------------------------------------------+
SELECT xpath_string(NULL, '/a');
+--------------------------+ | xpath_string(NULL, '/a') | +--------------------------+ | NULL | +--------------------------+
XPATH_STRING, XPATH, XML