1

我正在尝试体验 Oracle PLSQL 中的 XML 操作。所有示例都是修改存储在表列中的 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;

有人可以让我知道我做错了什么吗-如果可能的话,请举几个例子..

4

2 回答 2

1

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

以下是将标签“页面”插入正确位置的代码:

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

这是开始-结束的内部:

/*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;
于 2019-10-23T19:38:52.740 回答
1

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

很少有如何正确执行此操作的示例
1)$tmp/bookstore/book[@category eq "cooking"]插入到属性category = "cooking"
2)$tmp/bookstore/book[2]插入到 xml 中的第二本书中。
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

于 2019-10-24T06:42:00.153 回答