I am trying to get a taste of XML manipulation in Oracle PLSQL. All the examples are of modifying XML stored in table column but I am trying to modify a XML variable.
Here is my code -
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;
Can someone please let me know what I am doing wrong- if possible can you please show couple of examples..
Your query is returning Invalid target expression
.
Because this part $tmp/bookstore/book
is returning collection of book node not a single element.
Few examples how to do this correctly
1) $tmp/bookstore/book[@category eq "cooking"]
insert into book where attribute category = "cooking"
2) $tmp/bookstore/book[2]
insert into 2-nd boook in your xml.
3) Insert pages into each books
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)
Note
To get clob value from xml is better to use xmlserialize