Skip to main content
Version: V1.0.0

UPDATEXML

Description

This function is used to update the content of a node specified by a path in an XML string.

Syntax

UPDATEXML(xml_target, xpath_expr, new_xml)

Parameters

info

The xpath_expr parameter specifies the part of the xml_target parameter to be replaced with the new_xml parameter. If xpath_expr does not match any result or matches multiple results, the original result of xml_target is returned.

FieldDescription
xml_targetThe XML string to be updated. It can be an XML document or fragment.
xpath_exprThe XPath expression that specifies the path of the node to be updated.
new_xmlThe new XML fragment to be used to replace the node.

Return type

LONGTEXT

Examples

  • Use the UPDATEXML function to replace the </a> node with the <x>carrot</x> node in the XML string and return the updated XML string. The <a/><b/> indicates a root node that contains two nodes, <a> and <b>.

    SELECT UPDATEXML('<a/><b/>', '/a', '<x>carrot</x>');

    The return result is as follows:

    +----------------------------------------------+
    | UPDATEXML('<a/><b/>', '/a', '<x>carrot</x>') |
    +----------------------------------------------+
    | <x>carrot</x><b></b> |
    +----------------------------------------------+
    1 row in set (0.001 sec)
  • Use the UPDATEXML function to replace the a node under the root node with the <b>bbb</b> node in the XML string and return the updated XML string.

    SELECT UPDATEXML('<root><a>aaa</a></root>', '/root/a', '<b>bbb</b>');

    The return result is as follows:

    +---------------------------------------------------------------+
    | UPDATEXML('<root><a>aaa</a></root>', '/root/a', '<b>bbb</b>') |
    +---------------------------------------------------------------+
    | <root><b>bbb</b></root> |
    +---------------------------------------------------------------+
    1 row in set (0.001 sec)