首页 > 数据库 >KingbaseES 与 Oracle XML 语法比较

KingbaseES 与 Oracle XML 语法比较

时间:2023-02-03 12:12:30浏览次数:47  
标签:XML com employees t1 employee sh Oracle KingbaseES 节点

KingbaseES 内置支持 XML 相关操作,也可以通过xml2 插件进行扩展支持。以下通过例子介绍 KingbaseES XML 与Oracle 在用法上存在的一些差异。

一、数据准备

create table t1(content xml);
insert into t1 values( xml ('<Employees>
    <Employee emplid="1111" type="admin">
        <firstname>John</firstname>
        <lastname>Watson</lastname>
        <age>30</age>
        <email>[email protected]</email>
    </Employee>
    <Employee emplid="2222" type="admin">
        <firstname>Sherlock</firstname>
        <lastname>Homes</lastname>
        <age>32</age>
        <email>[email protected]</email>
    </Employee>
    <Employee emplid="3333" type="user">
        <firstname>Jim</firstname>
        <lastname>Moriarty</lastname>
        <age>52</age>
        <email>[email protected]</email>
    </Employee>
    <Employee emplid="4444" type="user">
        <firstname>Mycroft</firstname>
        <lastname>Holmes</lastname>
        <age>41</age>
        <email>[email protected]</email>
    </Employee>
</Employees>') );

二、XML节点标记方式

Expression

Description

nodename

选择所有名称为"nodename"的节点

/

选择根节点

//

从当前节点选择文档中相匹配的节点,无论他们在哪里

.

选择当前节点

..

选择当前节点的父节点

@

选择属性

employee

选择所有名称为"employee"的节点

employees/employee

选择所有子节点为employee的employees节点

//employee

选择所有employee的元素,无论他们在哪里

 

Path Expression

Result

/employees/employee[1]

选择第一个employee节点,它是employees的子节点。

/employees/employee[last()]

选择最后一个employee元素,它是employees的子节点

/employees/employee[last()-1]

选择是employees子元素的倒数第二个employee元素

//employee[@type='admin']

选择所有具有与'admin'的值的属性命名类型的employee元素

三、Extract

1、Oracle

SQL> select extract(content,'/Employees/Employee/firstname') from t1;

EXTRACT(CONTENT,'/EMPLOYEES/EMPLOYEE/FIRSTNAME')
--------------------------------------------------------------------------------
<firstname>John</firstname>
<firstname>Sherlock</firstname>
<firstname>Jim</firstname>
<firstname>Mycroft</firstname>

2、KingbaseES

test=# select extract(content,'/Employees/Employee/firstname') from t1;
                                                      extract                                                       
--------------------------------------------------------------------------------------------------------------------
 <firstname>John</firstname><firstname>Sherlock</firstname><firstname>Jim</firstname><firstname>Mycroft</firstname>
(1 row)

四、Xpath

 

XPath使用路径表达式来选择XML文档中的节点或节点列表。Oracle 没有该函数。

使用例子:

test=# select xpath('/Employees/Employee/firstname',content) from t1;
                                                          xpath                                                          
-------------------------------------------------------------------------------------------------------------------------
 {<firstname>John</firstname>,<firstname>Sherlock</firstname>,<firstname>Jim</firstname>,<firstname>Mycroft</firstname>}
(1 row)

test=# select xpath('/Employees/Employee/firstname/text()',content) from t1;
            xpath            
-----------------------------
 {John,Sherlock,Jim,Mycroft}
(1 row)

五、XMLTable

KingbaseES 有XMLTable 函数,Oracle 用 TABLE + XMLSequence 实现。

1、KingbaseES

test=# select c from t1,xmltable('/Employees/Employee/firstname' passing t1.content columns c xml path '.');
                c                
---------------------------------
 <firstname>John</firstname>
 <firstname>Sherlock</firstname>
 <firstname>Jim</firstname>
 <firstname>Mycroft</firstname>
(4 rows)

2、Oracle

select value(T) AS C from TABLE(XMLSequence(extract(XMLTYPE('<Employees>
    <Employee emplid="1111" type="admin">
        <firstname>John</firstname>
        <lastname>Watson</lastname>
        <age>30</age>
        <email>[email protected]</email>
    </Employee>
    <Employee emplid="2222" type="admin">
        <firstname>Sherlock</firstname>
        <lastname>Homes</lastname>
        <age>32</age>
        <email>[email protected]</email>
    </Employee>
    <Employee emplid="3333" type="user">
        <firstname>Jim</firstname>
        <lastname>Moriarty</lastname>
        <age>52</age>
        <email>[email protected]</email>
    </Employee>
    <Employee emplid="4444" type="user">
        <firstname>Mycroft</firstname>
        <lastname>Holmes</lastname>
        <age>41</age>
        <email>[email protected]</email>
    </Employee>
</Employees>'),'/Employees/Employee/firstname'))) T;

C
--------------------------------------------------------------------------------
<firstname>John</firstname>
<firstname>Sherlock</firstname>
<firstname>Jim</firstname>
<firstname>Mycroft</firstname>

 

标签:XML,com,employees,t1,employee,sh,Oracle,KingbaseES,节点
From: https://www.cnblogs.com/kingbase/p/15092861.html

相关文章

  • KingbaseES PLSQL 支持语句级回滚
    KingbaseES默认如果在PLSQL-block执行过程中的任何SQL语句导致错误,都会导致该事务的所有语句都被回滚,而Oracle则是语句级的回滚。KingbaseES为了更好的与Oracle兼容,新......
  • KingbaseES V8R6运维案例之---pg_statistic toast表故障修复
    ​案例说明:数据库在日常的维护过程中,执行表结构查询语句(\dt1),如下图所示,出现“missingchunknumber0fortoastvalue16259inpg_toast_2619”,从报错信息看和toast表......
  • KingbaseES V8集群运维案例之---系统用户修改密码或过期对ssh互信的影响
    案例说明:KingbaseV8主备流复制集群在通用机环境部署和运维,需要建立主机间的ssh互信,如果ssh互信被破坏,将导致集群故障。但有的生产环境为了系统安全需要,会配置密码管理策......
  • Oracle Business Intelligence Enterprise Edition(DataModel详解)
    数据模型编辑器数据模型编辑器使您能够将来自多个数据集的数据合并到单个XML数据结构中。来自多个数据源的数据集可以合并为顺序XML,也可以在行级别合并,以创建单个组......
  • oracle 自定义函数splitstr
     函数主体createorreplacetypetype_splitastableofvarchar2(4000) createorreplacefunctionsplitstr(p_stringvarchar2,......
  • Linux下重启Oracle数据库
    1、Linux下以Oracle帐户进入Linux系统。su -oracle   ---切换成oracle用户登录2、执行以下命令查看数据库监听器的状况:lsnrctlstatus3、执行以下命令停止数据库监......
  • Oracle查询
    --恢复表FLASHBACKTABLE"Student"TOBEFOREDROP;FLASHBACKTABLE"Grade"TOBEFOREDROP;--给字段起别名select"GradeID"as年纪编号,"GradeIdName"as"年纪名称"......
  • Oracle增删改
    --相表中插入一条数据INSERTINTO"Grade"("GradeID","GradeIdName")values(1,'20级中职');--如果按序插入,可以省略字段列表INSERTINTO"Grade"values(2,'19级中职');--插......
  • libxml2-master源码下载及编译
    libxml2源码下载地址https://gitlab.gnome.org/GNOME/libxml2/-/releasesCMake(mainlyforWindows)AnotheroptionforcompilinglibxmlisusingCMake:cmake-Et......
  • 从xml读取gps数据获取经纬高
         #!/usr/bin/python#-*-coding:UTF-8-*-fromxml.dom.minidomimportparseimportxml.dom.minidom"""数据输入xmlname文件名字no......