我正在使用循环创建父XML,我想在其中插入另一个XML。有没有办法将我的第二个XML插入第一个XML?
每个家庭的外圈
每个家庭的孙子的内在循环
SELECT XMLELEMENT ("grandkids"
,XMLELEMENT("kid1", 'ron')
,XMLELEMENT("age", '12')
) INTO TESTXML2 FROM DUAL;
END GRANDKIDS LOOP;
SELECT XMLELEMENT ("Family"
,XMLELEMENT("father", 'myFather')
,XMLELEMENT("mother", 'myMother')
,XMLELEMENT("children", '2')
<---INSERT TESTXML2 HERE--->
) INTO TESTXML FROM DUAL;
结束家庭循环;
如何将TESTXML2插入TESTXML1中?
可以在创建FamilyXML时插入它,也可以在将FamilyXML插入数据库后更新。
答案 0 :(得分:1)
您可以只引用您的testxml2
变量:
declare
testxml xmltype;
testxml2 xmltype;
begin
SELECT XMLELEMENT ("grandkids"
,XMLELEMENT("kid1", 'ron')
,XMLELEMENT("age", '12')
) INTO TESTXML2 FROM DUAL;
dbms_output.put_line(testxml2.getstringval());
SELECT XMLELEMENT ("Family"
,XMLELEMENT("father", 'myFather')
,XMLELEMENT("mother", 'myMother')
,XMLELEMENT("children", '2')
,testxml2
) INTO TESTXML FROM DUAL;
dbms_output.put_line(testxml.getstringval());
end;
/
<grandkids><kid1>ron</kid1><age>12</age></grandkids>
<Family><father>myFather</father><mother>myMother</mother><children>2</children><grandkids><kid1>ron</kid1><age>12</age></grandkids></Family>
PL/SQL procedure successfully completed.
或将所有嵌套的XMLElement调用放在一个查询中:
declare
testxml xmltype;
begin
SELECT XMLELEMENT ("Family"
,XMLELEMENT("father", 'myFather')
,XMLELEMENT("mother", 'myMother')
,XMLELEMENT("children", '2')
,XMLELEMENT ("grandkids"
,XMLELEMENT("kid1", 'ron')
,XMLELEMENT("age", '12')
)
) INTO TESTXML FROM DUAL;
dbms_output.put_line(testxml.getstringval());
end;
/
<Family><father>myFather</father><mother>myMother</mother><children>2</children><grandkids><kid1>ron</kid1><age>12</age></grandkids></Family>
PL/SQL procedure successfully completed.
如果您实际上是从表中获取数据,请查看XMLAgg,XMLForest等。