XML Document Functions

Describes some commonly used XML document functions like, ExtractValue() and UpdateXML().

MySQL supports two built-in functions that allows you to manipulate XML documents.

ExtractValue(xml, path) - Returns the first text value at a given XPath in a given XML document. In other words, the function actually applies "<path>/text()"

UpdateXML(xml, path, repl) - Returns a copy of a given XML document updated by replacing the sub-element identified a given path with a given placement XML document.

Examples of XML document functions, XmlFunctions.sql:

-- XmlFunctions.sql
-- Copyright (c) 2005 HerongYang.com. All Rights Reserved.
--
SELECT ExtractValue('<a><b>X</b><b>Y</b></a>','//b[2]');
SELECT ExtractValue('<a><b>X</b><b>Y</b></a>','/a/b[1]');
SELECT ExtractValue('<a>Hello <b>world</b>!</a>','/a');
SELECT ExtractValue('<a>Hello <b>world</b>!</a>','/a/text()');
SELECT ExtractValue('<a>Hello <b>world</b>!</a>','/a/b');
SELECT ExtractValue('<a>Hello <b>world</b>!</a>','/a/b/text()');

SELECT UpdateXML('<a><b>ccc</b><d></d></a>', '/a', '<e>fff</e>');
SELECT UpdateXML('<a><b>ccc</b><d></d></a>', '/b', '<e>fff</e>');
SELECT UpdateXML('<a><b>ccc</b><d></d></a>', '//b', '<e>fff</e>');
SELECT UpdateXML('<a><b>ccc</b><d></d></a>', '/a/d', '<e>fff</e>');

Output:

ExtractValue('<a><b>X</b><b>Y</b></a>','//b[2]')
Y
ExtractValue('<a><b>X</b><b>Y</b></a>','/a/b[1]')
X
ExtractValue('<a>Hello <b>world</b>!</a>','/a')
Hello  !
ExtractValue('<a>Hello <b>world</b>!</a>','/a/text()')
Hello  !
ExtractValue('<a>Hello <b>world</b>!</a>','/a/b')
world
ExtractValue('<a>Hello <b>world</b>!</a>','/a/b/text()')
world
UpdateXML('<a><b>ccc</b><d></d></a>', '/a', '<e>fff</e>')
<e>fff</e>
UpdateXML('<a><b>ccc</b><d></d></a>', '/b', '<e>fff</e>')
<a><b>ccc</b><d></d></a>
UpdateXML('<a><b>ccc</b><d></d></a>', '//b', '<e>fff</e>')
<a><e>fff</e><d></d></a>
UpdateXML('<a><b>ccc</b><d></d></a>', '/a/d', '<e>fff</e>')
<a><b>ccc</b><e>fff</e></a>

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

Commonly Used Functions

 Flow Control Functions

 Character String Functions

 Numeric Value Functions

 Date and Time Functions

 JSON Document Functions

XML Document Functions

 Spatial Data Functions

 Cast Function

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Window Functions for Statistical Analysis

 Use Index for Better Performance

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 System, User-Defined and Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

 Bulk Changes on Large Tables

 MySQL Server on macOS

 Installing MySQL Server on Linux

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB