如何循环和返回具有多个相同标签的XML值?

时间:2019-05-06 19:41:28

标签: xml oracle xmltype

我需要在以下XML中返回Fund_code值。有两个Fund / Fund_code,我的查询给我一个错误(给定XPath指向多个节点),我不知道如何解决它。

<ROWSET>
    <Fund>
        <Fund_code>F22</Fund_code>
    </Fund>
    <Fund>
        <Fund_code>F02</Fund_code>
    </Fund>
</ROWSET>

我尝试遍历XML元素,但不允许我这么做。

FOR I IN 1..2
  loop
  SELECT COUNT(EXTRACTVALUE(V_XML, '//Fund/Fund_code'))
  INTO V_fund_code
  FROM DUAL;
  DBMS_OUTPUT.PUT_LINE('GRANT_COUNT|'||V_fund_code);
  end loop;
END;

它给了我这个错误:

ORA-19025: EXTRACTVALUE returns value of only one node
ORA-06512: at line 34
19025. 00000 -  "EXTRACTVALUE returns value of only one node"
*Cause:    Given XPath points to more than one node.
*Action:   Rewrite the query so that exactly one node is returned.

2 个答案:

答案 0 :(得分:0)

使用以下方法找到解决方案。

FOR I IN 1..2
  loop
  SELECT COUNT(EXTRACTVALUE(V_XML, '//Fund['||I||']/Fund_code'))
  INTO V_fund_code
  FROM DUAL;
  DBMS_OUTPUT.PUT_LINE('GRANT_COUNT|'||V_fund_code);
  end loop;
END;

答案 1 :(得分:0)

BEGIN
  FOR i in (
             SELECT Fund_code
                FROM 
                   xmltable(
                              '//Fund'
                              PASSING XMLTYPE('<ROWSET>
                                                    <Fund>
                                                        <Fund_code>F22</Fund_code>
                                                    </Fund>
                                                    <Fund>
                                                        <Fund_code>F02</Fund_code>
                                                    </Fund>
                                                </ROWSET>')
                             COLUMNS
                                Fund_code varchar2(10) PATH '//*:Fund_code'                      
                                )

                           ) LOOP

               dbms_output.put_line(i.fund_code);

   END LOOP;
END;