Tuesday, October 07, 2008

Looping over a Set of Nodes with PL/SQL

Oracle's XMLType has the handy member function 'extract' which evaluates an XPath. Except it won't work if you XPath is a function call. So, if you want to count how many children there are of a node so you can iterate over them,
extract('count(/xpath/here/*)')
won't work. Here's the solution:
select count(*) into v_maxi from TABLE(XMLSequence(extract(p_xml,'/xpath/here')));               
FOR v_i IN 0..v_maxi-1 LOOP
v_xmlfrag := p_xml.extract('/xpath/here[count(preceding-sibling::*)='||v_i||']');
v_text := v_xmlfrag.extract('/record/stuff/text()').getStringVal();
END LOOP;

1 comment:

Anonymous said...

Thanks for this post, Peter. It certainly came in handy for me and I appreciate the help.


Taylor

Labels

Blog Archive

Contributors