问题陈述:
您想生成一个复杂的XML文档,其中的嵌套值位于不同的级别。
解决方案: Oracle具有许多用于从常规数据生成XML的功能和过程。为了演示用法,我将使用studentdata。我们将假设我们想将学生的学生ID用作根元素的XML属性,并添加有关费用的详细信息,将付款期限指定为属性,并添加货币详细信息。
Oracle函数XMLROOT,XMLELEMENT和XMLATTRIBUTE提供了对所需XML结构的完全控制。
XMLROOT提供必要的XML标头,以将我们的结果从XML片段转换为完整的XML文档。使用XMLROOT时导致
<?xml version="1.0"?>
XMLELEMENT的工作原理与dbms_output.put_line和连接命令几乎相同。XMLELEMENT将源列与提供的元素名称一起输出,以形成结果的XML构建块。您可以根据需要将对XMLELEMENT的调用串在一起(就像您可以连接任意数量的字符串一样),并且对其进行操作的表达式本身可以是对XMLELEMENT的另一个调用。
XMLATTRIBUTES指示Oracle将给定的列或表达式作为由XMLELEMENT的父调用创建的元素的属性。
XMLAttributes(student_id为“ student_id”)
SELECT XMLRoot( XMLElement("student", XMLAttributes(student_id as "student_id"), XMLElement("FirstName", first_name), XMLElement("LastName", last_name), XMLElement("fees", XMLAttributes('yearly' as "Period"), XMLElement("Amount", fees), XMLElement("Currency", 'USD')) ), VERSION '1.0') student_XML FROM students ;
输出结果
<?xml version="1.0"?> <student student_id="100"> <FirstName>SMITH</FirstName> <LastName>JAMES</LastName> <fees Period="yearly"> <Amount>24000</Amount> <Currency>USD</Currency> </fees> </student>
DROP TABLE students; COMMIT; CREATE TABLE students ( student_id NUMBER(6) , first_name VARCHAR2(20) , last_name VARCHAR2(25) , email VARCHAR2(40) , phone_number VARCHAR2(20) , join_date DATE , class_id VARCHAR2(20) , fees NUMBER(8,2) , professor_id NUMBER(6) , department_id NUMBER(4) ) ;
CREATE UNIQUE INDEX stu_id_pk ON students (student_id) ; INSERT INTO students VALUES (100,'SMITH','JAMES','SMITH.JAMES@notreal.com','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','JOHNSON.JOHN@notreal.com','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','WILLIAMS.ROBERT@notreal.com','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','BROWN.MICHAEL@notreal.com','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','JONES.WILLIAM@notreal.com','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','MILLER.DAVID@notreal.com','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','DAVIS.RICHARD@notreal.com','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','GARCIA.CHARLES@notreal.com','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','RODRIGUEZ.JOSEPH@notreal.com','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','WILSON.THOMAS@notreal.com','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','MARTINEZ.CHRISTOPHER@notreal.com','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','ANDERSON.DANIEL@notreal.com','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','TAYLOR.PAUL@notreal.com','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','THOMAS.MARK@notreal.com','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;