PL / SQL:计算xml中的节点数 [英] PL/SQL: Count number of nodes in xml
问题描述
我正在使用Oracle。
I am working with Oracle.
有没有办法计算使用PL / SQL的XML文件中的节点数量(包括后代)?
Is there a way to count the number of nodes (including descendants) within an XML file using PL/SQL?
我想能够将结果保存为一个变量,用作循环迭代器的上限。
I would like to be able to save the result as a variable to be used as an upper limit for a loop iterator.
我有以下xml,我想计算行节点内的节点数:
I have the following xml, and I want to count the number of nodes within the row node:
<row>
<date name="date1" id="101"></date>
<element1 name="ele1" id="111">
<stuff></stuff>
<stuff></stuff>
<stuff></stuff>
</element1>
<element2 name="ele2" id="121"></element2>
<element3 name="ele3" id="131></element15>
</row>
结果应该是7。
@johnbk我正在使用Oracle
@johnbk I am working with Oracle
这里的想法是,我得到的节点数后,我可以使用它:
The idea here is that after I get the number of nodes I can use it in:
nodeCount := 1;
FOR i IN 1 .. numNodes
LOOP
xpath1 := '/row/*[' || nodeCount || ']/@name';
SELECT EXTRACT(form_xml, xpath1) as other_name;
nodeCount := nodeCount +1;
END LOOP;
感谢您的帮助。
推荐答案
我想能够保存结果作为要用作循环迭代器上限的变量。
I would like to be able to save the result as a variable to be used as an upper limit for a loop iterator.
我猜这与你的另一个疑问?
你不需要知道节点的数量,因为你不必自己显式地循环xml。
You don't need to know the number of nodes as your don't have to explicitly loop xml by yourself. You might be trying to solve your real problem in a suboptimal way.
下面是如何找到您要查找的号码 XMLQUERY :
Below how you can find the number you're looking for with XMLQUERY:
declare
v_data constant xmltype := xmltype('<row>
<date name="date1" id="101"></date>
<element1 name="ele1" id="111">
<stuff></stuff>
<stuff></stuff>
<stuff></stuff>
</element1>
<element2 name="ele2" id="121"></element2>
<element3 name="ele3" id="131"></element3>
</row>');
v_count xmltype;
begin
select xmlquery('count($doc/row/descendant::*)'
passing v_data as "doc"
returning content)
into v_count from dual;
dbms_output.put_line('count = ' || v_count.getstringval);
end;
/
这篇关于PL / SQL:计算xml中的节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!