KingbaseES 原生XML系列二–XML数据操作函数(DELETEXML,APPENDCHILDXML,INSERTCHILDXML,INSERTCHILDXMLAFTER,INSERTCHILDXMLBEFORE,INSERTXMLAFTER,INSERTXMLBEFORE,UPDATEXML)

XML的简单使其易于在任何应用程序中读写数据,这使XML很快成为数据交换的一种公共语言。在不同平台下产生的信息,可以很容易加载XML数据到程序中并分析他,并以XML格式输出结果。
xml数据类型可以被用来存储XML数据。它比直接在一个text域中存储XML数据的优势在于,它会检查输入值的结构是不是良好,并且有支持函数用于在其上执行类型安全的操作。
本文详细介绍了KingbaseES中集成的相关xml数据操作函数使用。

准备数据:CREATE TABLE "public"."xmldata" ("id" integer NULL,"comm" varchar NULL,"xmlvarchar" varchar NULL,"xmldata" xml NULL,"XMLarray" xml NULL);INSERT INTO xmldata values(1, 'zhangsan' , 'aaa' , 'sql001rac001' , 'v8r3001v8r6001');INSERT INTO xmldata values(2, 'lisi' , 'bbb' , 'sql002rac002' , 'v8r3002v8r6002');INSERT INTO xmldata values(3, 'wangwu' , 'ccc' , 'sql003rac003' , 'v8r3003v8r6003');

xml函数列表

  • DELETEXML
  • APPENDCHILDXML
  • INSERTCHILDXML
  • INSERTCHILDXMLAFTER
  • INSERTCHILDXMLBEFORE
  • INSERTXMLAFTER
  • INSERTXMLBEFORE
  • UPDATEXML

json函数简介

DELETEXML

功能:

函数deletexml会删除xml_instance实例中与xpath表达式匹配的节点。

每次删除指定的一层节点,若一层含有多个节点,则该层所有节点都将被删除。若删除节点后父节点值为空,则父节点只显示结尾部分,否则显示完整父节点及父节点的其他值。

用法:

deletexml(xml_instance xml, xpath text)deletexml(xml_instance xml, xpath text, namespace _text)

示例:

test=# select deletexml(xmlvarchar , '/kes') , xmlvarchar from xmldata ; deletexml | xmlvarchar -----------+---------------- | aaa | bbb | ccc(3 行记录)test=# select deletexml(xmldata , '/kes/sql') , xmldata from xmldata ;deletexml|xmldata---------------------+----------------------------------------------- +| sql001rac001 rac001+||+| sql002rac002 rac002+||+| sql003rac003 rac003+|| (3 行记录)test=# select deletexml(xmlarray , '/kes/version') , xmlarray from xmldata ; deletexml |xmlarray -----------+----------------------------------------------------------------- | v8r3001v8r6001 | v8r3002v8r6002 | v8r3003v8r6003(3 行记录)

APPENDCHILDXML

功能:

函数appendchildxml将value_expr提供的值作为xpath节点的子节点追加到xml_instance中。成功则返回追加后的xml_instance数据,失败则返回ERROR。

用法:

appendchildxml(xml_instance xml,xpath text , value_expr xml)

示例:

test=# select appendchildxml(xmldata , '/kes' ,xmlvarchar) , xmldata , xmlvarchar from xmldata ; appendchildxml|xmldata| xmlvarchar ---------------------+-----------------------------------------------+---------------- +| sql001rac001 | aaa sql001+| |rac001+| |aaa +| || |+| sql002rac002 | bbb sql002+| |rac002+| |bbb +| || |+| sql003rac003 | ccc sql003+| |rac003+| |ccc +| || | (3 行记录)

INSERTCHILDXML

功能:

函数insertchildxml将value_expr提供的值作为xpath指定节点的子节点插入到xml_instance中。成功则返回插入后的xml_instance数据,失败则返回ERROR。

用法:

insertchildxml(xml_instance xml,xpath text, child_expr text , value_expr xml)insertchildxml(xml_instance xml,xpath text , child_expr text ,value_expr xml, namespace _text)

示例:

test=# select insertchildxml(xmldata , '/kes' , 'cluster' , 'nodes') ,xmldata from xmldata ; insertchildxml |xmldata----------------------------+-----------------------------------------------  +| sql001rac001 sql001 +|rac001 +|nodes+| | +| sql002rac002 sql002 +|rac002 +|nodes+| | +| sql003rac003 sql003 +|rac003 +|nodes+| | (3 行记录)-- 数组数据插入test=# select insertchildxml(XMLarray , '/kes' , 'cluster' , 'nodes') , XMLarray from xmldata ;insertchildxml|XMLarray ------------------------------+-----------------------------------------------------------------  +| v8r3001v8r6001 v8r3001+|v8r6001+|nodes+| | +| v8r3002v8r6002 v8r3002+|v8r6002+|nodes+| | +| v8r3003v8r6003 v8r3003+|v8r6003+|nodes+| | (3 行记录)

INSERTCHILDXMLAFTER

功能:

函数insertchildxmlafter将value_expr提供的一个或多个集合元素作为xpath指定的目标父元素的子元素插入到child_expr指定的现有集合元素之后。成功则返回插入后的xml_instance数据,失败则返回ERROR。

用法:

insertchildxmlafter(xml_instance xml,xpath text, child_expr text , value_expr xml)insertchildxmlafter(xml_instance xml,xpath text , child_expr text ,value_expr xml,namespace _text)

示例:

test=# select insertchildxmlafter(xmldata , '/kes','sql','nodes') ,xmldata from xmldata ;insertchildxmlafter |xmldata----------------------------+-----------------------------------------------  +| sql001rac001 sql001 +|nodes+|rac001 +| | +| sql002rac002 sql002 +|nodes+|rac002 +| | +| sql003rac003 sql003 +|nodes+|rac003 +| | (3 行记录)-- 数组数据插入test=#select insertchildxmlafter(xmlarray ,'/kes','version[1]' ,'nodes') ,xmlarray from xmldata ; insertchildxmlafter|xmlarray ------------------------------+-----------------------------------------------------------------  +| v8r3001v8r6001 v8r3001+|nodes+|v8r6001+| | +| v8r3002v8r6002 v8r3002+|nodes+|v8r6002+| | +| v8r3003v8r6003 v8r3003+|nodes+|v8r6003+| | (3 行记录)

INSERTCHILDXMLBEFORE

功能:

函数insertchildxmlbefore将value_expr提供的一个或多个集合元素作为xpath指定的目标父元素的子元素插入到child_expr指定的现有集合元素之前。成功则返回插入后的xml_instance数据,失败则返回ERROR。

用法:

insertchildxmlbefore(xml_instance xml,xpath text, child_expr text , value_expr xml)insertchildxmlbefore(xml_instance xml,xpath text , child_expr text , value_expr xml,namespace _text)

示例:

test=# select insertchildxmlbefore(xmldata , '/kes','sql','nodes') ,xmldata from xmldata ;insertchildxmlbefore|xmldata----------------------------+-----------------------------------------------  +| sql001rac001 nodes+|sql001 +|rac001 +| | +| sql002rac002 nodes+|sql002 +|rac002 +| | +| sql003rac003 nodes+|sql003 +|rac003 +| | (3 行记录)-- 数组数据插入test=# select insertchildxmlbefore(xmlarray ,'/kes','version[1]' ,'nodes') ,xmlarray from xmldata ; insertchildxmlbefore |xmlarray ------------------------------+-----------------------------------------------------------------  +| v8r3001v8r6001 nodes+|v8r3001+|v8r6001+| | +| v8r3002v8r6002 nodes+|v8r3002+|v8r6002+| | +| v8r3003v8r6003 nodes+|v8r3003+|v8r6003+| | (3 行记录)

INSERTXMLAFTER

功能:

函数insertxmlafter将value_expr提供的值插入到xpath指定的节点之后。成功则返回插入后的xml_instance数据,失败则返回ERROR。

用法:

insertxmlafter(xml_instance xml,xpath text , value_expr xml)insertxmlafter(xml_instance xml,xpath text , value_expr xml,namespace _text)

示例:

test=# select insertxmlafter(xmldata ,'/kes/sql' ,'nodes') ,xmldatafrom xmldata ; insertxmlafter |xmldata----------------------------+-----------------------------------------------  +| sql001rac001 sql001 +|nodes+|rac001 +| | +| sql002rac002 sql002 +|nodes+|rac002 +| | +| sql003rac003 sql003 +|nodes+|rac003 +| | (3 行记录)-- 数组数据插入test=# select insertxmlafter(xmlarray ,'/kes/version[1]' ,'nodes') , xmlarray from xmldata ;insertxmlafter|xmlarray ------------------------------+-----------------------------------------------------------------  +| v8r3001v8r6001 v8r3001+|nodes+|v8r6001+| | +| v8r3002v8r6002 v8r3002+|nodes+|v8r6002+| | +| v8r3003v8r6003 v8r3003+|nodes+|v8r6003+| | (3 行记录)

INSERTXMLBEFORE

功能:

函数insertxmlbefore将value_expr提供的值插入到xpath指定的节点之前。成功则返回插入后的xml_instance数据,失败则返回ERROR。

用法:

insertxmlbefore(xml_instance xml,xpath text , value_expr xml)insertxmlbefore(xml_instance xml,xpath text , value_expr xml,namespace _text)

示例:

test=# select insertxmlbefore(xmldata ,'/kes/sql' ,'nodes') ,xmldatafrom xmldata ;insertxmlbefore |xmldata----------------------------+-----------------------------------------------  +| sql001rac001 nodes+|sql001 +|rac001 +| | +| sql002rac002 nodes+|sql002 +|rac002 +| | +| sql003rac003 nodes+|sql003 +|rac003 +| | (3 行记录)-- 数组数据插入test=# select insertxmlbefore(xmlarray ,'/kes/version[1]' ,'nodes') , xmlarray from xmldata ; insertxmlbefore|xmlarray ------------------------------+-----------------------------------------------------------------  +| v8r3001v8r6001 nodes+|v8r3001+|v8r6001+| | +| v8r3002v8r6002 nodes+|v8r3002+|v8r6002+| | +| v8r3003v8r6003 nodes+|v8r3003+|v8r6003+| | (3 行记录)

UPDATEXML

功能:

函数updatexml将xml_instance实例中xpath指定的节点内容替换为value_expr提供的值,成功则返回更新后的xml_instance实例,失败则返回ERROR。

用法:

updatexml(xml_instance xml, xpath text, value_expr xml)updatexml(xml_instance xml, xpath text, value_expr text)updatexml(xml_instance xml, xpath text, value_expr text, namespace text)

示例:

test=# select updatexml(xmlvarchar ,'/kes' ,'xxx' ) , xmlvarchar from xmldata ; updatexml| xmlvarchar ----------------+---------------- xxx | aaa xxx | bbb xxx | ccc(3 行记录)