zoukankan      html  css  js  c++  java
  • Oracle XMLTable 使用教程与实例

    Oracle 10g开始,甲骨文公司新增了XQueryXMLTable两个功能作为处理XML的武器。 XMLQuery一样,您可以使用XQuery语言构造XML数据和查询XML和关系数据。你可以使用XMLTable从XQuery查询结果创建关系表和列。

    本文我们将了解Oracle XMLTable函数,并且通过例子介绍XMLTable函数的用法。

    考虑到员工会有一些XML数据,所以我们创建一个EMPLOYEES表:

    Create TABLE EMPLOYEES

    (

    id NUMBER,

    data XMLTYPE

    )

    表创建完成后,我们往表里插入一些数据:

     SQL Code 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27

     

    Insert INTO EMPLOYEES
         VALUES (1, xmltype ('<Employees>
        <Employee emplid="1111" type="admin">
            <firstname>John</firstname>
            <lastname>Watson</lastname>
            <age>30</age>
            <email>johnwatson@sh.com</email>
        </Employee>
        <Employee emplid="2222" type="admin">
            <firstname>Sherlock</firstname>
            <lastname>Homes</lastname>
            <age>32</age>
            <email>sherlock@sh.com</email>
        </Employee>
        <Employee emplid="3333" type="user">
            <firstname>Jim</firstname>
            <lastname>Moriarty</lastname>
            <age>52</age>
            <email>jim@sh.com</email>
        </Employee>
        <Employee emplid="4444" type="user">
            <firstname>Mycroft</firstname>
            <lastname>Holmes</lastname>
            <age>41</age>
            <email>mycroft@sh.com</email>
        </Employee>
    </Employees>'));

    注意:XML包含了员工的相关数据,在我们开始学习之前我们首先明确几个数据:

    1、有4名员工在我们的XML文件

    2、每个员工都有通过属性定义一个唯一的员工id emplid

    3、每个员工也有一个属性type,定义雇员是否是管理员或用户。

    4、每个员工都有四个子节点: firstname , lastname , age和email

    5、年龄是多少

    现在我们可以使用Oracle XMLTable函数从XML中检索不同的信息。

     

    1、学习XPath表达式

    使用XMLTable函数之前最好知道一点关于XPath。XPath使用路径表达式来选择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元素

     

    其他更多的表达式可以参考Oracle官方手册

     

    2、Oracle XMLTable函数的基础知识

    读取Employees中所有firstname和lastname

    在这个查询中,我们使用XMLTable函数从EMPLOYEES表解析XML内容。

    select t.id, x.*

    from employees t,

    xmltable('/Employees/Employee' passing t.data columns firstname

    varchar2(30) path 'firstname',

    lastname varchar2(30) path 'lastname') x

    where t.id = 1;

    注XMLTable函数的语法:

    XMLTable('<XQuery>'

    PASSING <xml column>

    COLUMNS <new column name> <column type> PATH <XQuery path>)

    XMLTABLE函数包含一个XQuery行表达式和由一个或多个列表达式组成的COLUMNS子句。在上面的语句中,行表达式是 XPath /Employees/Employee。PASSING子句中的t.data指的是employees表中的XML列中的数据。

    COLUMNS 子句用于将XML数据转换成关系数据,这里每个参数都定义了一个列名和SQL数据类型。在上面的查询中,我们定义了firstname 和 lastname列并指向PATH的firstname 和 lastname或者选定的节点。

    输出:

     

    使用text()读取节点值

    在上面的教程中,我们读取到了firstname / lastname节点。通常我们还需要获取节点的文本值,下面的例子中,我们选取/Employees/Employee/firstname路径,并使用text()获取节点的值。

    下面查询employees中所有的firstname

    select t.id, x.*

    from employees t,

    xmltable('/Employees/Employee/firstname' passing t.data columns

    firstname varchar2(30) path 'text()') x

    where t.id = 1

    输出:

    不仅仅是text()表达式,Oracle还提供了其他很多有用的表达式,如 item(), node(), attribute(), element(), document-node(), namespace(), text(), xs:integer, xs:string。

     

    读取所选节点的属性

    XML节点定了相关属性,我们也可以读取到节点的这些属性,下面的查询是找出employee节点的type属性:

    select emp.id, x.*

    from employees emp,

    xmltable('/Employees/Employee' passing emp.data columns firstname

    varchar2(30) path 'firstname',

    type varchar2(30) path '@type') x;

    输出:

     

    使用ID读取特定的记录

    select t.id, x.*

    from employees t,

    xmltable('/Employees/Employee[@emplid=2222]' passing t.data columns

    firstname varchar2(30) path 'firstname',

    lastname varchar2(30) path 'lastname') x

    where t.id = 1;

    输出:

     

    读取所有类型是admin的员工的firstname 和 lastname

    select t.id, x.*

    from employees t,

    xmltable('/Employees/Employee[@type="admin"]' passing t.data columns

    firstname varchar2(30) path 'firstname',

    lastname varchar2(30) path 'lastname') x

    where t.id = 1;

    输出:

     

    读取年龄超过40的所有员工的firstname 和 lastname

    select t.id, x.*

    from employees t,

    xmltable('/Employees/Employee[age>40]' passing t.data columns

    firstname varchar2(30) path 'firstname',

    lastname varchar2(30) path 'lastname',

    age varchar2(30) path 'age') x

    where t.id = 1;

    输出:

     

    本文由UncleToo翻译整理,转载请注明出处!

    原文(英文)地址:http://viralpatel.net/blogs/oracle-xmltable-tutorial/

  • 相关阅读:
    zabbix3.0配置服务器流量告警
    centos6.5环境Redis下载及编译安装
    tomcat报错catalina.sh: line 401: /usr/java/jdk1.7.52/bin/java: No such file or directory
    zabbix报警Too many processes on zabbix server
    tomcat报错:java.net.SocketException: Permission denied["http-nio-80"]
    tomcat启动报错:Injection of autowired dependencies failed
    java的split的坑,会忽略空值
    教训:任何的程序脚本,即便你认为再没有问题,也要测试一下再上线。
    如何修改HDFS的备份数
    sqoop遇到的问题
  • 原文地址:https://www.cnblogs.com/mellowsmile/p/4647501.html
Copyright © 2011-2022 走看看