准备测试数据:
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;
101张老师数学专业硕士
102王老师英语专业硕士
是不是很清晰呢?要是在前台构建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
显示的信息如下:
101张老师 数学专业硕士001< /ID>高等数学< ID>002离散数学< course>003线性代数< /course>
102王老师 英语专业硕士004< /ID>大学英语
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
--------------------------------------------------
ADAMS
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(/tech-oracle/URL, SPARE/index.html)
--------------------------------------------------------
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文档。示例如下:
SELECT SYS_XMLAGG(SYS_XMLGEN(ename)) xml_content
2 FROM emp;
XML_CONTENT
----------------------------------------------------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
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
-----------------------------------------------------
CLARK
MILLER
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
---------------------------------------------------
ALLEN 1600
WARD 1250
MARTIN 1250
BLAKE 2850
TURNER 1500
JAMES 950
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
--------------------------------------------------
SCOTT
3000
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
-------------------------------------------
CLARK
2450
KING
5000
CLARK
1300
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)
------------------------------------------
28-DEC-03
SQL> SELECT xmlelement("Emp",
2 xmlattributes(empno AS "ID", ename)) Employee
3 FROM emp WHERE deptno=10;
EMPLOYEE
---------------------------------------------
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))
------------------------------------------------
SCOTT
3000
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(
The Ruling Class
), XMLTYPE(
Diabolique
), XMLTYPE(
8 1/2
))
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('
index.jsp
1
index2.jsp
2
'))
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
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;
101张老师数学专业硕士
102王老师英语专业硕士
是不是很清晰呢?要是在前台构建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
显示的信息如下:
101张老师 数学专业硕士001< /ID>高等数学< ID>002离散数学< course>003线性代数< /course>
102王老师 英语专业硕士004< /ID>大学英语
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
--------------------------------------------------
ADAMS
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(/tech-oracle/URL, SPARE/index.html)
--------------------------------------------------------
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文档。示例如下:
SELECT SYS_XMLAGG(SYS_XMLGEN(ename)) xml_content
2 FROM emp;
XML_CONTENT
----------------------------------------------------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
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
-----------------------------------------------------
CLARK
MILLER
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
---------------------------------------------------
ALLEN 1600
WARD 1250
MARTIN 1250
BLAKE 2850
TURNER 1500
JAMES 950
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
--------------------------------------------------
SCOTT
3000
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
-------------------------------------------
CLARK
2450
KING
5000
CLARK
1300
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)
------------------------------------------
28-DEC-03
SQL> SELECT xmlelement("Emp",
2 xmlattributes(empno AS "ID", ename)) Employee
3 FROM emp WHERE deptno=10;
EMPLOYEE
---------------------------------------------
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))
------------------------------------------------
SCOTT
3000
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(
The Ruling Class
), XMLTYPE(
Diabolique
), XMLTYPE(
8 1/2
))
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('
index.jsp
1
index2.jsp
2
'))
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