.. _dbms_xmlquery:

=================
dbms_xmlquery
=================


插件dbms_xmlquery简介
------------------------

插件dbms_xmlquery是KingbaseES 的一个扩展插件，兼容Oracle的dbms_xmlquery系统包。

dbms_xmlquery系统包提供了一组子程序，用于将数据库表数据转换为XML格式文本。

- 插件名为 dbms_xmlquery

- 插件版本 V1.0


插件dbms_xmlquery加载方式
----------------------------

该插件通过create extension dbms_xmlquery手动创建。


插件dbms_xmlquery的参数配置
-----------------------------

dbms_xmlquery扩展插件无需配置任何参数。


插件dbms_xmlquery使用方法
---------------------------

dbms_xmlquery系统包包含的子程序及简介如下表所示：

.. table:: 
   :class: longtable 

   +-------------------------------+----------------------------------------------------------------------------------+
   | 子程序                        | 描述                                                                             |
   +===============================+==================================================================================+
   | CLOSECONTEXT                  | 关闭或取消分配特定的查询上下文                                                   |
   +-------------------------------+----------------------------------------------------------------------------------+
   | GETNUMROWSPROCESSED           | 返回查询处理的行数                                                               |  
   +-------------------------------+----------------------------------------------------------------------------------+
   | GETXML                        | 生成XML文档                                                                      |
   +-------------------------------+----------------------------------------------------------------------------------+
   | NEWCONTEXT                    | 创建查询上下文并返回上下文句柄                                                   |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETCOLLIDATTRNAME             | 为集合元素的分隔符标记ID设置属性名称                                             |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETDATAHEADER                 | 设置XML数据标头                                                                  |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETENCODINGTAG                | 设置XML文档中编码处理指令                                                        |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETMAXROWS                    | 设置转换为XML的最大行数                                                          |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETROWIDATTRNAME              | 为行封闭标记的ID设置属性名称                                                     |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETROWIDATTRVALUE             | 指定标量列，其值将配分配给行封闭标记的ID属性                                     |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETROWSETTAG                  | 设置封闭XML数据集的标记                                                          |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETROWTAG                     | 设置将XML元素括起来的标记                                                        |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETSKIPROWS                   | 设置跳过的行数                                                                   |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETSQLTOXMLNAMEESCAPING       | 如果映射到XML标识符的SQL对象名称不是有效的XML标识符，则启用或禁用XML标记的转义   |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETSTYLESHEETHEADER           | 设置样式表标题                                                                   |
   +-------------------------------+----------------------------------------------------------------------------------+
   | SETTAGCASE                    | 指定生成XML标记的大小写样式                                                      |
   +-------------------------------+----------------------------------------------------------------------------------+
   | USENULLATTRIBUTEINDICATOR     | 指定是否使用XML属性来表示NULL                                                    |
   +-------------------------------+----------------------------------------------------------------------------------+
   | USETYPEFORCOLLELEMTAG         | 指示XSU使用集合元素的类型名作为集合元素的标记名                                  |
   +-------------------------------+----------------------------------------------------------------------------------+



常量
^^^^^^
DBMS_XMLQUERY系统包在调用时(包括指定参数时)要使用的多个常量，下表列出了常量兼容范围与兼容格式类型。

.. table:: DBMS_XMLQUERY系统包常量
   :widths: 2 4 4 3
   :class: longtable 

    +----------------------+----------------------+--------------+---------------------------+
    | 常量                 | Oracle               | KES          |       描述                |
    +======================+======================+==============+===========================+
    | DB_ENCODING          | _                    | UTF-8        | 设置数据库字符编码        |
    +----------------------+----------------------+--------------+---------------------------+
    | DEFAULT_ROWSETTAG    | ROWSET               | rowset       | 设置从结果集生成的XML的   |
    |                      |                      |              | 元素的标记名称（在大多数  |
    |                      |                      |              | 情况下，根节点的标记名称）|
    |                      |                      |              | -- ROWSET                 |
    +----------------------+----------------------+--------------+---------------------------+
    | DEFAULT_ERRORTAG     | ERROR                | 不兼容       | 包含引发错误的默认标记    |
    |                      |                      |              | – ERROR                   |
    +----------------------+----------------------+--------------+---------------------------+
    | DEFAULT_ROWIDATTR    | NUM                  | num          | 设置数据库XML元素的基数   |
    |                      |                      |              | 属性的默认名称            |
    +----------------------+----------------------+--------------+---------------------------+
    | DEFAULT_ROWTAG       | ROW                  | row          | 设置数据库元素的默认名称  |
    |                      |                      |              | 标记                      |
    +----------------------+----------------------+--------------+---------------------------+
    | DEFAULT_DATE_FORMAT  | MM/dd/yyyy HH:mm:ss  | 不兼容       | 默认日期掩码              |
    |                      |                      |              | –‘MM/DD/YYYY HH:MM:SS’    |
    +----------------------+----------------------+--------------+---------------------------+
    | ALL_ROWS             |  -1                  | 2147483647   | 指定输出需要包含所有行    |
    +----------------------+----------------------+--------------+---------------------------+
    | NONE                 |  0                   | 不兼容       | 指定输出不应包含任何      |
    |                      |                      |              | XML元数据（例如不包含DTD）|
    +----------------------+----------------------+--------------+---------------------------+
    | DTD                  |  1                   | 不兼容       | 指定需要生成DTD           |
    +----------------------+----------------------+--------------+---------------------------+
    | SCHEMA               |  2                   | 不兼容       | 指定生成的XML模式         |
    +----------------------+----------------------+--------------+---------------------------+
    | LOWER_CASE           |  1                   | LOWER_CASE 1 | 使用小写标记名            |
    +----------------------+----------------------+--------------+---------------------------+
    | UPPER_CASE           |  2                   | LOWER_CASE 2 | 使用大写标记名            |
    +----------------------+----------------------+--------------+---------------------------+

函数
^^^^^^

getnumrowsprocessed
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的GetNumrowsProcessed函数，返回查询处理的行数。

**语法**

.. code::

  FUNCTION  getNumRowsProcessed(ctxHdl IN ctxType) RETURN NUMBER;

**参数说明**

ctxHdl：被声明且打开的上下文查询句柄。

**返回类型**

NUMBER

**示例**

.. code::

   DECLARE
     qryCtx dbms_xmlquery.ctxHandle;
     result CLOB;
     colms number;
   BEGIN
     qryCtx := dbms_xmlquery.newContext('select * from my_table');
     result := dbms_xmlquery.getXML(qryCtx);
     colms :=dbms_xmlquery.getnumrowsprocessed(qryCtx);
     dbms_output.put_line(colms);
     qryCtx := dbms_xmlquery.newContext('select * from my_table');
     dbms_xmlquery.setSkiprows(qryCtx,5);
     dbms_xmlquery.setmaxrows(qryCtx,5);
     colms :=dbms_xmlquery.getnumrowsprocessed(qryCtx);
     dbms_output.put_line(colms);
     result := dbms_xmlquery.getXML(qryCtx);
     colms :=dbms_xmlquery.getnumrowsprocessed(qryCtx);
     dbms_output.put_line(colms);
     dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   8
   0
   3
   PL/SQL procedure successfully completed.

getXML
~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的getXML过程及函数，用于创建上下文，执行查询，返回XML并关闭上下文，这是一个非常有用的函数，上下文不需要显示的打开或关闭。

**语法**

.. code::

  FUNCTION  getXML(ctxHdl IN ctxType, metaType IN NUMBER := NONE) RETURN CLOB;
  PROCEDURE getXML(ctxHdl IN ctxType, xDoc IN CLOB, metaType IN NUMBER := NONE);
  FUNCTION  getXML(sqlQuery IN VARCHAR2, metaType IN NUMBER := NONE) RETURN CLOB;
  FUNCTION  getXML(sqlQuery IN CLOB, metaType IN NUMBER := NONE) RETURN CLOB;

**参数说明**

ctxHdl：被声明且打开的上下文查询句柄。

**返回类型**

CLOB

**示例**

.. code::

   DECLARE
     qryCtx dbms_xmlquery.ctxHandle;
     qryCtx1 dbms_xmlquery.ctxHandle;
     result CLOB;
     result1 CLOB;
   BEGIN
     qryCtx := dbms_xmlquery.newContext('select * from my_table');
     result := dbms_xmlquery.getXML(qryCtx);
     result1 := dbms_xmlquery.getXML('select * from my_table');
     qryCtx1 := dbms_xmlquery.newContext('select * from test2');
     dbms_xmlquery.getXML(qryCtx1,result);
     dbms_xmlquery.closeContext(qryCtx);
     dbms_xmlquery.closeContext(qryCtx1);
     end;

newContext
~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的newContext函数，创建并保存一个上下文，并将其上下文句柄返回。

**语法**

.. code::

  FUNCTION newContext(sqlQuery IN VARCHAR2) RETURN ctxType;
  FUNCTION newContext(sqlQuery IN CLOB) RETURN ctxType;

**参数说明**

sqlQuery：查询结果集以及查询语句。

**返回类型**

ctxtype

**示例**

.. code::

   DECLARE
     qryCtx dbms_xmlquery.ctxHandle;
     result CLOB;
     newCtx dbms_xmlquery.ctxHandle;
     result1 CLOB;
     result2 CLOB;
   BEGIN
     qryCtx := dbms_xmlquery.newContext('select * from test2');
     result := dbms_xmlquery.getXML(qryCtx);
     insert into xml_test values(result);
     newctx := dbms_xmlquery.newContext('select * from xml_test');
     result2 := dbms_xmlquery.getXML(newctx);
     dbms_xmlquery.closeContext(qryCtx);
     dbms_xmlquery.closeContext(newCtx);
    end;



存储过程
^^^^^^^^^^

closeContext
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的CloseContext过程，用于关闭或解除分配特定的查询上下文。

**语法**

.. code::

  procedure CloseContext（ctxHdl in ctxtype）；

**参数说明**

ctxHdl：被声明且打开的上下文查询句柄。

**示例**

.. code::

   create extension dbms_xmlquery;
   create table test1 (id int, name text);
   create table test2 (id int, name text);
   
   insert into test1 values(1, 'aaa'), (2, 'bbb');
   insert into test2 values(3, 'ccc'), (4, 'ddd');
   
   create table my_table (ID number, NAME varchar2(40), DEPT varchar2(40));
   insert into my_table values(1,'test_1','math'),
   (2,'test_2','chinese'),
   (3,'test_3','sql_oracle'),
   (4,'test_4','chinese'),
   (5,'test_5','math'),
   (6,'test_6','sql_oracle'),
   (7,'test_7','sql_oracle'),
   (8,'test_8','chinese');
   
    create table city1(id number,name varchar2(40));
    create table city2(id number,name varchar2(40));
    insert into city1(id,name) values(1,'广州');
    insert into city1(id,name) values(2,'深圳');
    insert into city1(id,name) values(3,'珠海');
    insert into city1(id,name) values(4,'北京');
    insert into city1(id,name) values(5,'上海');
    insert into city2(id,name) values(1,'南京');
    insert into city2(id,name) values(2,'无锡');
    insert into city2(id,name) values(3,'苏州');
    insert into city2(id,name) values(4,'北京');
    insert into city2(id,name) values(5,'上海');
   
   create table my_students (sid int primary key, sname varchar2(40),subj varchar2(255)[]);
   
   insert into my_students(sid,sname,subj) values(1,'test_1-CN-张三', array['化学','物理','生物']);
   insert into my_students(sid,sname,subj) values(2,'test_2-CN-李四', array['化学','物理','生物']);
   insert into my_students(sid,sname,subj) values(3,'test_3-EN-aili', array['math','chinese','english']);
   insert into my_students(sid,sname,subj) values(4,'test_4-EN-BOB', array['SQL-server','KingbaseES','Oracle']);
   insert into my_students(sid,sname,subj) values(5,'test_1-JP-あい', array['イキ','ふへ','なにぬふ']);
   
   create table my_NULL (id int primary key, nullname varchar2(50) default null,dept varchar2(50)default null);
   insert into my_NULL (id,nullname,dept) values(1,'test1','math');
   insert into my_NULL (id,nullname,dept) values(2,'test2',null);
   insert into my_NULL (id,nullname,dept) values(3,null,'chinese');
   insert into my_NULL (id,nullname,dept) values(4,null,null);
   insert into my_NULL (id,nullname,dept) values(5,'test5','nothing');
   
   DECLARE
   qryCtx1 dbms_xmlquery.ctxHandle;
   BEGIN
   qryCtx1 := dbms_xmlquery.newContext('select * from my_table');
   dbms_xmlquery.closeContext(qryCtx1);
   END；

setCollIdAttrName
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setCollIdAttrName过程，用于设置集合元素的分隔符标记的ID属性名称，设置NULL或者空的字符串标记，则会忽略行的ID属性。

**语法**

.. code::

  PROCEDURE setCollIdAttrName(ctxHdl IN ctxType, attrName IN VARCHAR2);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- attrName：设置的属性名称。

**示例**

.. code::

   DECLARE
     qryCtx dbms_xmlquery.ctxHandle;
     result CLOB;
   BEGIN
     qryCtx := dbms_xmlquery.newContext('select * from my_students');
     dbms_xmlquery.SETCOLLIDATTRNAME(qryCtx,'kes');
     result := dbms_xmlquery.getXML(qryCtx);
     dbms_output.put_line(result);
     dbms_xmlquery.closeContext(qryCtx);
    end;


setDataHeader
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setDataHeader过程，用于设置XML数据标头，数据标头是添加到查询生成的XML实体开头的部分，将这两个实体括在参数中，使用最后指定的数据标头，传递参数NULL会取消设置数据标头。

**语法**

.. code::

   PROCEDURE setDataHeader(ctxHdl IN ctxType, header IN CLOB := null, tag IN VARCHAR2 := null);
  

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Header：设置数据头的内容。
- Tag：rowset级别周围外的标签名称，默认为：<DOCUMENT>

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   header CLOB:='<?KingbaseES-xml:version=1.0?>';
   tag varchar2(20):='my_table';
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from my_table');
   dbms_xmlquery.SETDATAHEADER(qryCtx,header,tag);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_output.put_line(qryCtx);
   dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   <?xml version = '1.0'?>
   <my_table>
      <?KingbaseES-xml:version=1.0?>
   
   <ROWSET>
         <ROW num="1">
            <ID>1</ID>
            <NAME>test_1</NAME>
   
   <DEPT>math</DEPT>
         </ROW>
         <ROW num="2">
            <ID>2</ID>
   
   <NAME>test_2</NAME>
            <DEPT>chinese</DEPT>
         </ROW>
         <ROW
   num="3">
            <ID>3</ID>
            <NAME>test_3</NAME>
   
   <DEPT>sql_oracle</DEPT>
         </ROW>
         <ROW num="4">
            <ID>4</ID>
   
   <NAME>test_4</NAME>
            <DEPT>chinese</DEPT>
         </ROW>
         <ROW
   num="5">
            <ID>5</ID>
            <NAME>test_5</NAME>
   
   <DEPT>math</DEPT>
         </ROW>
         <ROW num="6">
            <ID>6</ID>
   
   <NAME>test_6</NAME>
            <DEPT>sql_oracle</DEPT>
         </ROW>
         <ROW
   num="7">
            <ID>7</ID>
            <NAME>test_7</NAME>
   
   <DEPT>sql_oracle</DEPT>
         </ROW>
         <ROW num="8">
            <ID>8</ID>
   
   <NAME>test_8</NAME>
            <DEPT>chinese</DEPT>
         </ROW>
   
   </ROWSET>
   </my_table>

setMaxRows
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setMaxRows过程，设置转换为XML的最大行数，默认情况下，不设置最大行数。

**语法**

.. code::

  PROCEDURE setMaxRows (ctxHdl IN ctxType, rows IN NUMBER);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Rows：给定的最大查询行数。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from my_table');
   dbms_xmlquery.setMAXrows(qryCtx,2);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.setMAXrows(qryCtx,2);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
    /
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW num="1">
         <ID>1</ID>
   
   <NAME>test_1</NAME>
         <DEPT>math</DEPT>
      </ROW>
      <ROW num="2">
   
   <ID>2</ID>
         <NAME>test_2</NAME>
         <DEPT>chinese</DEPT>
   
   </ROW>
   </ROWSET>
   
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW num="3">
         <ID>3</ID>
   
   <NAME>test_3</NAME>
         <DEPT>sql_oracle</DEPT>
      </ROW>
      <ROW num="4">
   
   <ID>4</ID>
         <NAME>test_4</NAME>
         <DEPT>chinese</DEPT>
   
   </ROW>
   </ROWSET>
   
   PL/SQL procedure successfully completed.

setRowIdAttrName
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setRowIdAttrName过程，用于设置包含标记行的属性名称，如果传值为NULL或空值，则会省略。

**语法**

.. code::

  PROCEDURE setRowIdAttrName(ctxHdl IN ctxType, attrName IN VARCHAR2);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Attrname：给定的属性值名称。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   attrName varchar2(40):='wangbo';
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from test1');
   dbms_xmlquery.SETROWIDATTRNAME(qryCtx,attrName);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW wangbo="1">
         <ID>1</ID>
   
   <NAME>aaa</NAME>
      </ROW>
      <ROW wangbo="2">
         <ID>2</ID>
   
   <NAME>bbb</NAME>
      </ROW>
   </ROWSET>


setCollIdAttrName
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setCollIdAttrName过程，用于设置包含标记行的属性名称，如果传值为NULL或空值，则会省略属性。

**语法**

.. code::

  PROCEDURE setCollIdAttrName(ctxHdl IN ctxType, attrName IN VARCHAR2);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- attrName：设置的属性名称。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   attrName varchar2(40):='wangbo';
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from test1');
   dbms_xmlquery.SETROWIDATTRNAME(qryCtx,attrName);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW wangbo="1">
         <ID>1</ID>
   
   <NAME>aaa</NAME>
      </ROW>
      <ROW wangbo="2">
         <ID>2</ID>
   
   <NAME>bbb</NAME>
      </ROW>
   </ROWSET>

setRowIdAttrValue
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setRowIdAttrValue过程，指定将其值分配给行封装标记的属性的索引列，传值为NULL或者空字符，则为属性分配基数值（0,1,2…）

**语法**

.. code::

  PROCEDURE setRowIdAttrValue(ctxHdl IN ctxType, colName IN VARCHAR2);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Colname：设置查询结果中的索引列。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   attrName varchar2(40):='KingbaseES-xml';
   colName varchar2(40):='NAME';
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from test1');
   dbms_xmlquery.SETROWIDATTRNAME(qryCtx,attrName);
   dbms_xmlquery.SETROWIDATTRVALUE(qryCtx,colName);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW KingbaseES-xml="aaa">
         <ID>1</ID>
   
   <NAME>aaa</NAME>
      </ROW>
      <ROW KingbaseES-xml="bbb">
         <ID>2</ID>
   
   <NAME>bbb</NAME>
      </ROW>
   </ROWSET>
   PL/SQL procedure successfully completed.

setRowsetTag
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setRowsetTag过程，用于设置rowset数据集标签的名称。设置根节点的标记值（rowset）的默认名称修改。

**语法**

.. code::

  PROCEDURE setRowsetTag(ctxHdl IN ctxType, tag IN VARCHAR2);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Tag：给定的标签名称。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   tag varchar2(20):='wangbo';
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from test2');
   dbms_xmlquery.SETROWSETTAG(qryCtx,tag);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
    /
   <?xml version = '1.0'?>
   <wangbo>
      <ROW num="1">
         <ID>3</ID>
   
   <NAME>ccc</NAME>
      </ROW>
      <ROW num="2">
         <ID>4</ID>
   
   <NAME>ddd</NAME>
      </ROW>
   </wangbo>
   PL/SQL procedure successfully completed.

setRowTag
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setRowTag过程，用于设置行（row）级标签名称。

**语法**

.. code::

  PROCEDURE setRowTag(ctxHdl IN ctxType, tag IN VARCHAR2);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Tag：给定的标签名称。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   tag varchar2(20):='wangbo';
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from test2');
   dbms_xmlquery.SETROWTAG(qryCtx,tag);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   <?xml version = '1.0'?>
   <ROWSET>
      <wangbo num="1">
         <ID>3</ID>
   
   <NAME>ccc</NAME>
      </wangbo>
      <wangbo num="2">
         <ID>4</ID>
   
   <NAME>ddd</NAME>
      </wangbo>
   </ROWSET>
   PL/SQL procedure successfully completed.

setSkipRows
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setSkipRows过程，设置要跳过的行数，默认情况下，不跳过任何行。

**语法**

.. code::

   PROCEDURE setSkipRows(ctxHdl IN ctxType, rows IN NUMBER);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Rows：需要跳过的行数。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from my_table');
   dbms_xmlquery.setskiprows(qryCtx,2);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.setskiprows(qryCtx,2);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW num="3">
         <ID>3</ID>
   
   <NAME>test_3</NAME>
         <DEPT>sql_oracle</DEPT>
      </ROW>
      <ROW num="4">
   
   <ID>4</ID>
         <NAME>test_4</NAME>
         <DEPT>chinese</DEPT>
      </ROW>
   
   <ROW num="5">
         <ID>5</ID>
         <NAME>test_5</NAME>
   
   <DEPT>math</DEPT>
      </ROW>
      <ROW num="6">
         <ID>6</ID>
   
   <NAME>test_6</NAME>
         <DEPT>sql_oracle</DEPT>
      </ROW>
      <ROW num="7">
   
   <ID>7</ID>
         <NAME>test_7</NAME>
         <DEPT>sql_oracle</DEPT>
      </ROW>
   
   <ROW num="8">
         <ID>8</ID>
         <NAME>test_8</NAME>
   
   <DEPT>chinese</DEPT>
      </ROW>
   </ROWSET>
   
   <?xml version = '1.0'?>
   <ROWSET/>
   PL/SQL procedure successfully completed.

setStylesheetHeader
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setStylesheetHeader过程，用于设置生成XML文档的样式表的标头，传递NULL值到uri会取消设置样式表标题和样式表类型

**语法**

.. code::

  PROCEDURE setStylesheetHeader(ctxHdl IN ctxType, uri IN VARCHAR2, type IN VARCHAR2 := 'text/xsl');

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Uri：设置stylesheet的uri。
- Type：设置stylesheet类型。

**示例**

.. code::

    DECLARE
      qryCtx dbms_xmlquery.ctxHandle;
      result CLOB;
      uri varchar2(40):='KES';
      tag varchar2(20):='KingbaseES-xml';
      BEGIN
      qryCtx := dbms_xmlquery.newContext('select * from test1');
      dbms_xmlquery.SETSTYLESHEETHEADER(qryCtx,uri,tag);
      result := dbms_xmlquery.getXML(qryCtx);
      dbms_output.put_line(result);
      dbms_xmlquery.closeContext(qryCtx);
     end;
    /
   <?xml version = '1.0'?>
   <?xml-stylesheet href="KES"
   type="KingbaseES-xml"?>
   <ROWSET>
      <ROW num="1">
         <ID>1</ID>
   
   <NAME>aaa</NAME>
      </ROW>
      <ROW num="2">
         <ID>2</ID>
   
   <NAME>bbb</NAME>
      </ROW>
      <ROW num="3">
         <ID>3</ID>
   
   <NAME>vvv</NAME>
      </ROW>
   </ROWSET>
   PL/SQL procedure successfully completed.

setTagCase
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setTagCase过程，用于指定XML标记的大小写。

**语法**

.. code::

  PROCEDURE setTagCase(ctxHdl IN ctxType, tCase IN NUMBER);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Tcase：给定大小写参数的值，0位默认不操作，1为小写，2为大写。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from test1');
   dbms_xmlquery.SETTAGCASE(qryCtx,2);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_output.put_line('-------------------------------------------');
   qryCtx := dbms_xmlquery.newContext('select * from test1');
   dbms_xmlquery.SETTAGCASE(qryCtx,1);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_xmlquery.closeContext(qryCtx);
    end;
     /
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW NUM="1">
         <ID>1</ID>
   
   <NAME>aaa</NAME>
      </ROW>
      <ROW NUM="2">
         <ID>2</ID>
   
   <NAME>bbb</NAME>
      </ROW>
   </ROWSET>
   
   -------------------------------------------
   <?xml version = '1.0'?>
   <rowset>
      <row num="1">
         <id>1</id>
   
   <name>aaa</name>
      </row>
      <row num="2">
         <id>2</id>
   
   <name>bbb</name>
      </row>
   </rowset>
   
   PL/SQL procedure successfully completed.

useNullAttributeIndicator
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的useNullAttributeIndicator过程，此过程使用XML属性来指定它是NULL或指定是否省略XML文档中的某些实体。

**语法**

.. code::

  PROCEDURE useNullAttributeIndicator(ctxHdl IN ctxType, flag IN BOOLEAN := true);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Flag：开启NULL值显示开关true表示开启，false表示关闭。

**示例**

.. code::

     DECLARE
        qryCtx dbms_xmlquery.ctxHandle;
        result CLOB;
      BEGIN
        qryCtx := dbms_xmlquery.newContext('select * from my_null');
        dbms_xmlquery.useNullAttributeIndicator(qryCtx,false);
        result := dbms_xmlquery.getXML(qryCtx);
        dbms_output.put_line(result);
        dbms_output.put_line('-------------------------------------------');
       qryCtx := dbms_xmlquery.newContext('select * from my_null');
        dbms_xmlquery.useNullAttributeIndicator(qryCtx,true);
        result := dbms_xmlquery.getXML(qryCtx);
        dbms_output.put_line(result);
        dbms_output.put_line('-------------------------------------------');
        result :=dbms_xmlquery.getxml('select * from my_null');
        dbms_output.put_line(result);
       dbms_xmlquery.closeContext(qryCtx);
     end;
    /
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW num="1">
         <ID>1</ID>
         <NULLNAME>test1</NULLNAME>
         <DEPT>math</DEPT>
      </ROW>
      <ROW num="2">
         <ID>2</ID>
   
   <NULLNAME>test2</NULLNAME>
      </ROW>
      <ROW num="3">
         <ID>3</ID>
         <DEPT>chinese</DEPT>
      </ROW>
      <ROW num="4">
         <ID>4</ID>
      </ROW>
      <ROW num="5">
         <ID>5</ID>
   
   <NULLNAME>test5</NULLNAME>
         <DEPT>nothing</DEPT>
      </ROW>
   </ROWSET>
   
   -------------------------------------------
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW num="1">
         <ID>1</ID>
         <NULLNAME>test1</NULLNAME>
         <DEPT>math</DEPT>
      </ROW>
      <ROW num="2">
         <ID>2</ID>
   
   <NULLNAME>test2</NULLNAME>
         <DEPT NULL="TRUE"/>
      </ROW>
      <ROW num="3">
         <ID>3</ID>
         <NULLNAME NULL="TRUE"/>
         <DEPT>chinese</DEPT>
      </ROW>
      <ROW num="4">
         <ID>4</ID>
   
   <NULLNAME NULL="TRUE"/>
         <DEPT NULL="TRUE"/>
      </ROW>
      <ROW num="5">
         <ID>5</ID>
         <NULLNAME>test5</NULLNAME>
         <DEPT>nothing</DEPT>
      </ROW>
   </ROWSET>
   
   -------------------------------------------
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW num="1">
         <ID>1</ID>
         <NULLNAME>test1</NULLNAME>
         <DEPT>math</DEPT>
      </ROW>
      <ROW num="2">
         <ID>2</ID>
   
   <NULLNAME>test2</NULLNAME>
      </ROW>
      <ROW num="3">
         <ID>3</ID>
         <DEPT>chinese</DEPT>
      </ROW>
      <ROW num="4">
         <ID>4</ID>
      </ROW>
      <ROW num="5">
         <ID>5</ID>
   
   <NULLNAME>test5</NULLNAME>
         <DEPT>nothing</DEPT>
      </ROW>
   </ROWSET>
   
   PL/SQL procedure successfully completed.

useTypeForCollElemTag
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的useTypeForCollElemTag过程，此过程指定是否将集合元素的类型名称用作该元素的标记名称。默认情况下，集合元素的标记名称是集合类型，false时集合标记的名称后跟集合的标记名称_item。

**语法**

.. code::

  PROCEDURE useTypeForCollElemTag(ctxhdl IN ctxhandle, flag IN boolean := true);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Flag：设置是否开启使用类型名作为集合元素的标签名称。

**示例**

.. code::

   DECLARE
    result CLOB;
    qryCtx dbms_xmlquery.ctxHandle;
   BEGIN
    qryCtx := dbms_xmlquery.newContext('select * from my_students');
    dbms_xmlquery.useTypeForCollElemTag(qryCtx);
    result := dbms_xmlquery.getXML(qryCtx);
    dbms_output.put_line(result);
    dbms_xmlquery.closeContext(qryCtx);
   end;

setEncodingTag
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setEncodingTag过程，设置XML文档的编码处理指令。

**语法**

.. code::

  PROCEDURE setEncodingTag(ctxHdl IN ctxType, enc IN VARCHAR2:= 'UTF-8');

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Enc：编码格式名称。

**示例**

.. code::

   DECLARE
   qryCtx dbms_xmlquery.ctxHandle;
   result CLOB;
   enc varchar2(20) := 'UTF-8';
   BEGIN
   qryCtx := dbms_xmlquery.newContext('select * from test1');
   dbms_xmlquery.SETENCODINGTAG(qryCtx,enc);
   result := dbms_xmlquery.getXML(qryCtx);
   dbms_output.put_line(result);
   dbms_output.put_line(qryCtx);
   dbms_xmlquery.closeContext(qryCtx);
    end;
    /
   <?xml version = '1.0' encoding = 'UTF-8'?>
   <ROWSET>
      <ROW num="1">
   
   <ID>1</ID>
         <NAME>aaa</NAME>
      </ROW>
      <ROW num="2">
         <ID>2</ID>
   
   <NAME>bbb</NAME>
      </ROW>
      <ROW num="3">
         <ID>3</ID>
   
   <NAME>vvv</NAME>
      </ROW>
   </ROWSET>
   5

setSQLToXMLNameEscaping
~~~~~~~~~~~~~~~~~~~~~~~~

实现兼容Oracle包dbms_xmlquery中的setSQLToXMLNameEscaping过程，如果映射到XML标识符的SQL对象名称不是有效的XML标识符，则关闭或打开XML标记转义。

**语法**

.. code::

  PROCEDURE setSQLToXMLNameEscaping(ctxHdl IN ctxType, flag IN BOOLEAN := true);

**参数说明**

- ctxHdl：被声明且打开的上下文查询句柄。
- Flag：是否开启非法行标签字符转义，元素内容强制开启。

**示例**

.. code::

     DECLARE
      qryCtx dbms_xmlquery.ctxhandle;
      result CLOB;
      BEGIN
      qryCtx := dbms_xmlquery.newContext('select 1 "<" from dual');
      dbms_xmlquery.SETSQLTOXMLNAMEESCAPING(qryCtx,false);
      result := dbms_xmlquery.getXML(qryCtx);
      dbms_output.put_line(result);
      dbms_output.put_line('--------------------------------');
      qryCtx := dbms_xmlquery.newcontext('select 1 "<" from dual');
      dbms_xmlquery.SETSQLTOXMLNAMEESCAPING(qryCtx,true);
      result := dbms_xmlquery.getXML(qryCtx);
      dbms_output.put_line(result);
      dbms_xmlquery.closeContext(qryCtx);
     end;
     /
   <?xml version = '1.0'?>
   <ERROR>oracle.xml.sql.OracleXMLSQLException: Character
   '<' is not allowed in an XML tag name.</ERROR>
   
   --------------------------------
   <?xml version = '1.0'?>
   <ROWSET>
      <ROW num="1">
         <_x003C_>1</_x003C_>
   
   </ROW>
   </ROWSET>
   PL/SQL procedure successfully completed.

插件dbms_xmlquery卸载方法
---------------------------

通过drop extension dbms_xmlquery手动卸载。


插件dbms_xmlquery升级方法
---------------------------

若该插件有升级版本则通过 alter extension 来升级插件。

**示例**，由 1.0 升级到 1.1

.. code::

  ALTER EXTENSION dbms_xmlquery UPDATE TO '1.1';