XML查询 INSERT/DELETE XML 个元素

XMLQuery to INSERT/DELETE XML Elements

我正在尝试在 Oracle PLSQL 中体验 XML 操作。所有示例都是修改存储在 table 列中的 XML 但我正在尝试修改 XML 变量。

这是我的代码 -

declare
l_xml XMLTYPE;
l_clob CLOB;

begin
l_xml:=XMLTYPE('<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>');

select XMLQuery('copy $tmp := $p modify insert node 
<Pages>260 </Pages>
as last into $tmp/bookstore/book
return $tmp
'
passing l_xml as "p"
returning CONTENT).getCLobVal() into l_clob from dual;
DBMS_OUTPUT.PUT_LINE(l_clob);

end;

有人可以告诉我我做错了什么吗?如果可能的话,请举几个例子..

我希望这会有所帮助(我使用 Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production)并且对我来说没问题:

这是将标记 'Pages' 插入正确位置的代码:

select INSERTCHILDXML(l_xml,
   '/bookstore/book', 'Pages',
   XMLType('<Pages>260</Pages>')) into l_xml
   from dual;

这里是 begin-end 的内部:

/*from this part it is the same code as you have given us*/
declare
l_xml XMLTYPE;
l_clob CLOB;

begin
l_xml:=XMLTYPE('<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>');
/*to this part it is the same code as you have given us*/

/*this is just one example how you can add <Pages> tag*/
select INSERTCHILDXML(l_xml,
   '/bookstore/book', 'Pages',
   XMLType('<Pages>260</Pages>')) into l_xml
   from dual;

/*this part is so you can see what you did :)*/
select l_xml.getClobVal() 
into l_clob from dual;

DBMS_OUTPUT.PUT_LINE(l_clob);

end;

您的查询正在返回 Invalid target expression。 因为这部分 $tmp/bookstore/book 返回的是书籍节点的集合,而不是单个元素。

几个如何正确执行此操作的示例
1) $tmp/bookstore/book[@category eq "cooking"] 插入到 book where attribute category = "cooking"
2) $tmp/bookstore/book[2] 插入您 xml.
的第 2 本书 3) 在每本书中插入页面

XMLQuery('copy $tmp := $p modify 
(for $book in $tmp/bookstore/book return insert node <Pages>260 </Pages> into $book)
return $tmp'
passing l_xml as "p"
returning CONTENT)

备注
要从 xml 获取 clob 值最好使用 xmlserialize