oracle xml数据构建 XMLType相关函数

准备测试数据:

create table teacher (id varchar2(10),name varchar2(20),remark varchar2(50));
create table course (id varchar2(10),name varchar2(50),tid varchar2(10));

insert into teacher values ('101','张老师','数学专业硕士);
insert into teacher('102','王老师','英语专业硕士');

insert into course values ('001','高等数学','101');
insert into course values ('002','离散数学','101');
insert into course values ('003','线性代数','101');
insert into course values ('004','大学英语','102');
commit;

下面先查询出老师的信息,根据具体的名称来组织。

select XMLELEMENT( NAME DATA,--构建根节点
XMLATTRIBUTES( 'teacher' AS TYPE ),--构建属性
XMLELEMENT("ID",id),--构建子节点
XMLELEMENT("NAME",name),
XMLELEMENT("REMARK",remark)
).GETSTRINGVAL() --转换为字符类型,否则为clob
from teacher;

<DATA TYPE="teacher"><ID>101</ID><NAME>张老师</NAME><REMARK>数学专业硕士</REMARK></DATA>

<DATA TYPE="teacher"><ID>102</ID><NAME>王老师</NAME><REMARK>英语专业硕士</REMARK></DATA>

是不是很清晰呢?要是在前台构建xml信息代码会显得很臃肿!

上面的代码还可以简化些!
select XMLELEMENT( NAME DATA,
XMLATTRIBUTES( 'teacher' AS TYPE ),
XMLFOREST(Id "ID",Name "NAME",remark "REMARK")
).GETSTRINGVAL()
from teacher;

那么如果存在主从表的关系,如何使用这个函数处理呢?
看下面的例子,想显示老师的信息,以及他所讲的课程信息。

select XMLELEMENT( NAME DATA,
XMLATTRIBUTES( 'teacher' AS TYPE ),
XMLELEMENT("ID",a.id),
XMLELEMENT("NAME",a.name),
XMLELEMENT("REMARK",a.remark),
XMLAGG(XMLELEMENT( "course", --在内部又构建一个跟节点做为扩展
XMLELEMENT( "ID", b.Id),
XMLELEMENT( "NAME",b.Name)
)
)).GETSTRINGVAL()
from teacher a,course b
Where a.id = b.tid(+)
Group By a.id,a.name,a.remark;--注意必须有group by

显示的信息如下:
<DATA TYPE="teacher"><ID>101</ID><NAME>张老师</NAME> <REMARK>数学专业硕士</REMARK><course><ID>001< /ID><NAME>高等数学</NAME></course><course>< ID>002</ID><NAME>离散数学</NAME></course>< course><ID>003</ID><NAME>线性代数</NAME>< /course></DATA>

<DATA TYPE="teacher"><ID>102</ID><NAME>王老师</NAME> <REMARK>英语专业硕士</REMARK><course><ID>004< /ID><NAME>大学英语</NAME></course></DATA>

2.1 DEPTH(n)
 DEPTH(n):该函数用于返回XML方案中UNDER_PATH路径所对应的相对层数,其中参数n用于指定相对层数。示例如下:
SQL>  SELECT PATH(1),DEPTH(2) FROM resource_view
    2  WHERE UNDER_PATH(res, '/sys/schema/OE', 1)=1
    3  AND UNDER_PATH(res, '/sys/schema/OE', 2)=1;
PATH(1)                                                  DEPTH(2)

----------------------------                               --------
/www.oracle.com                                                1
/www.oracle.com/xwarehouses.xsd                      2

2.2 EXISTSNODE(XMLType_instance,Xpath_string)
 EXISTSNODE(XMLType_instance,Xpath_string):该函数用于确定特定的XML节点的路径是否存在,返回0表示节点不存在,返回1表示节点存在。其中参数XMLType_instance用于指定XMLType实例,Xpath_string用于指定XML节点路径。示例如下:
SQL> SELECT existsnode(VALUE(p),'/PurchaseOrder/User') node
    2   FROM xmltable p;
          NODE

           ------------
           1

2.3 EXTRACT(XMLType_instance,Xpath_string)
  EXTRACT(XMLType_instance,Xpath_string):该函数用于返回XML节点路径下的相应内容。其中参数XMLType_instance用于指定XMLType实例,Xpath_string用于指定XML节点路径。示例如下:
SQL> SELECT extract(value (p),'/PurchaseOrder/User') content
    2   FROM xmltable p;
CONTENT

--------------------------------------------------
<User>ADAMS</User>

2.4 EXTRACTVALUE(XMLType_instance,Xpath_string)
  EXTRACTVALUE(XMLType_instance,Xpath_string):该函数用于返回特定XML节点路径的数据。其中参数XMLType_instance用于指定XMLType实例,Xpath_string用于指定XML节点路径。示例如下:
SQL> SELECT extractvalue(value(p),'/PurchaseOrder/User')  data
    2    FROM xmltable  p;
DATA

-------------------------------------------------
ADAMS

2.5 PATH(correction_integer)
  PATH(correction_integer):该函数用于返回特定XML资源所对应的相对路径,参数correction_integer用于指定路径层数。示例如下:
SQL> SELECT PATH(1), DEPTH(2)    FROM  resource_view
    2   WHERE UNDER_PATH(res, '/sys/schemas/OE', 1)=1
    3   AND INDER_PATH(res,'/sys/schemas/OE',2)=1;
PATH(1)                                                   DEPTH(2)

----------------------------                             --------

/www.oracle.com                                                1
/www.oracle.com/xwarehouses.xsd                      2

2.6 SYS_DBURIGEN({column|attribute})
  SYS_DBURIGEN({column|attribute}):该函数用于根据列或者属性生成类型为DBUrlType的URL。参数column用于指定列名,attribute用于指定对象属性名。示例如下:
SQL> SELECT sys_dburigen(ename) url    FROM emp WHERE deptno=10;
URL(URL, SPARE)

--------------------------------------------------------
DBURITYPE('/PUBLIC/EMP/ROW[ENAME='CLARK'] /ENAME', NULL)
DBURITYPE('/PUBLIC/EMP/ROW[ENAME='KING'] /ENAME', NULL)
DBURITYPE('/PUBLIC/EMP/ROW[ENAME='MILLER'] /ENAME', NULL)

2.7 SYS_XMLAGG(expr[,fmt])
  SYS_XMLAGG(expr[,fmt]):该函数用于汇总所有XML文档,并生成一个XML文档。示例如下:
<SQL> SELECT SYS_XMLAGG(SYS_XMLGEN(ename))  xml_content
      2  FROM emp;
XML_CONTENT

----------------------------------------------------

<?xml version="1.0"?>
<ROWSET>
<ENAME>SMITH</ENAME>
<ENAME>ALLEN</ENAME>
<ENAME>WARD</ENAME>
<ENAME>JONES</ENAME>
<ENAME>MARTIN</ENAME>
<ENAME>BLAKE</ENAME>
<ENAME>CLARK</ENAME>
<ENAME>SCOTT</ENAME>
<ENAME>KING</ENAME>
<ENAME>TURNER</ENAME>
<ENAME>ADAMS</ENAME>
<ENAME>JAMES</ENAME>
<ENAME>FORD</ENAME>
<ENAME>MILLER</ENAME>
</ROWSET>

2.8 SYS_XMLGEN(expr[,fmt])
  SYS_XMLGEN(expr[,fmt]):该函数用于根据数据库表的行和列生成XMLType实例。参数expr用于指定列名,fmt用于指定格式。示例如下:
SQL> SELECT sys_xmlgen(ename) xml FROM emp WHERE deptno=10;
XML
-----------------------------------------------------
<?xml version="1.0"?>
<ENAME>CLARK</ENAME>
<?xml version="1.0"?>
<ENAME>MILLER</ENAME>

2.9 UPDATEXML(XMLType_instance,Xpath_string,value_expr)
  UPDATEXML(XMLType_instance,Xpath_string,value_expr):该函数用于更新特定XMLType实例相应的节点路径的内容。其中参数XMLType_instance用于指定XMLType实例,Xpath_string用于指定XML节点路径,value_expr用于指定新值。示例如下:
SQL> UPDATE xmltable p SET p=UPDATEXML(value (p),
    2       '/PurchaseOrder/User/text() ','SCOTT');

2.10 XMLAGG(XMLType_instance[ORDER BY sort_list])
  XMLAGG(XMLType_instance[ORDER BY sort_list]):该函数用于汇总多个XML块,并生成XML文档。其中参数XMLType_instance用于指定XMLType实例,sort_list用于生成指定的排序方式。示例如下:
SQL> SELECT xmlagg(xmlelement("employee",ename||' '||sal))  xml
    2   FROM emp WHERE deptno=30;
XML
---------------------------------------------------
<employee>ALLEN  1600</employee>
<employee>WARD  1250</employee>
<employee>MARTIN  1250</employee>
<employee>BLAKE  2850</employee>
<employee>TURNER  1500</employee>
<employee>JAMES  950</employee>

2.11 XMLCOLATTVAL(value_expr[,value_expr2],...)
  XMLCOLATTVAL(value_expr[,value_expr2],...):该函数用于生成XML块,参数value_expr用于指定列名或者别名作为属性名。示例如下:
SQL> SELECT xmlelement ("emp",xmlcolattval (ename,sal))    xml
    2   FROM emp WHERE ename='SCOTT';
XML
--------------------------------------------------
<emp>
<column name="ENAME">SCOTT</column>
<column name="SAL">3000</column>
</emp>

2.12 XMLCONCAT(XMLType_instance1[,XMLType_instance2],...)
  XMLCONCAT(XMLType_instance1[,XMLType_instance2],...):该函数用于连接多个XMLType实例,并生成新的XMLType实例。参数XMLType_instance用于指定XML实例。示例如下:
SQL> SELECT xmlconcat(xmlelement("ename",ename),
    2      xmlelement("sal",sal))  xml
    3  FROM emp WHERE deptno=10;
XML
-------------------------------------------
<ename>CLARK</ename>
<sal>2450</sal>

<ename>KING</ename>
<sal>5000</sal>

<ename>CLARK</ename>
<sal>1300</sal>

2.13 XMLELEMENT(identifier[,xml_attribute_clause][,value_expr])
  XMLELEMENT(identifier[,xml_attribute_clause][,value_expr]):该函数用于返回XMLType的实例。其中参数identifier用于指定元素名,参数xml_attribute_clause用于指定元素属性子句,参数value_expr用于指定元素值。示例如下:
SQL> select xmlelement ("DATE",sysdate)  from dual;
XMLELEMENT ("DATE",SYSDATE)
------------------------------------------
<DATE>28-DEC-03</DATE>
SQL> SELECT xmlelement("Emp",
    2      xmlattributes(empno AS "ID", ename))  Employee
    3   FROM emp WHERE deptno=10;
EMPLOYEE
---------------------------------------------
<Emp ID="7782" ENAME="CLARK"/>
<Emp ID="7839" ENAME="KING"/>
<Emp ID="7934" ENAME="MILLER"/>

2.14 XMLFOREST(value_expr1[,value_expr2],...)
  XMLFOREST(value_expr1[,value_expr2],...):该函数用于返回XML块。示例如下:
SQL> SELECT xmlelement ("Employee",xmlforest(ename,sal))
    2    FROM emp WHERE empno=7788;
XMLELEMENT ("EMPLOYEE",XMLFOREST(ENAME,SAL))
------------------------------------------------
<Employee>
    <ENAME>SCOTT</ENAME>
    <SAL>3000</SAL>
</Employee>

2.15 XMLSEQUENCE(xmltype_instance)
  XMLSEQUENCE(xmltype_instance):该函数用于返回XMLType实例中顶级节点以下的VARRAY元素。示例如下:
SQL> SELECT xmlsequence(extract(value(x),
    2      '/PurchaseOrder/LineItem/*'))  varray FROM xmltable x;
VARRAY
--------------------------------------------------
XMLSEQUENCETYPE(XMLTYPE(<LineItem ItemNumber="1">
    <Description>The Ruling Class</Description>
    <Part Id="715515012423" UnitPrice="29.95" Quantity="2"/>
</LineItem>
),  XMLTYPE(<LineItem ItemNumber="2">
  <Description>Diabolique</Description>
  <Part Id="037429135020" UnitPrice="29.95" Quantity="3"/>
</LineItem>
),  XMLTYPE(<LineItem ItemNumber="3">
  <Description>8 1/2</Description>
  <Part Id="037429135624" UnitPrice="39.95" Quantity="4"/>
</LineItem>
))

2.16 XMLTRANSFORM(xmltype_instance,xsl_ss)
  XMLTRANSFORM(xmltype_instance,xsl_ss):该函数用于将XMLType实例按照XSL样式进行转换,并生成新的XMLType实例。示例如下:
SQL> SELECT XMLTRANSFORM(w.warehouse-spec,x.coll).GetClobVal()
    2    FROM warehouse w,xsl_tab x
    3    WHERE w.warehouse_name='San Francisco';

 

 

 

 

CREATE TABLE purchase_order( po_no number(9),po_file xmltype)

insert into purchase_order values(68,XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>
<out>
    <record id="1">
        <FileName>index.jsp</FileName>
        <FileID>1</FileID>
    </record>
    <record id="2">
        <FileName>index2.jsp</FileName>
        <FileID>2</FileID>
    </record>
</out>'))


SELECT extract(po_file,'out/record/FileName').getStringVal() AS FileName,extract(po_file,'out/record/FileID').getStringVal() AS FileID  FROM  purchase_order

SELECT extract(po_file,'out/record/@id').getStringVal() AS RECORD  FROM  purchase_order


SELECT extract(po_file,'out/record/FileName').getStringVal() AS FileName FROM  purchase_order


select extractValue(value(i),'/FileName') AS FileName
 from purchase_order x,
 table(XMLSequence(extract(x.po_file,'out/record/FileName'))) i

你可能感兴趣的:(oracle,sql,xml,String,insert,Path)