Number of node elements in XML [message #320176] |
Wed, 14 May 2008 04:35 |
Seshagiri
Messages: 13 Registered: October 2007 Location: United Kingdom
|
Junior Member |
|
|
Hi,
I would like to loop through an XML doc. For this I need to know the number of nodes present.
Is there a way of doing this using XPath ?
Regards,
Sesha
|
|
|
|
|
Re: Number of node elements in XML [message #321153 is a reply to message #320176] |
Mon, 19 May 2008 04:33 |
hobbes
Messages: 173 Registered: January 2006
|
Senior Member |
|
|
An example using XPath, though I cannot see much business utility for this...
SQL> DECLARE
2 x XMLTYPE :=
3 xmlType('<DATA>
4 <LINE>A</LINE>
5 <LINE>B</LINE>
6 <LINE>C</LINE>
7 <LINE>D</LINE>
8 </DATA>');
9 CountNode NUMBER(4);
10 BEGIN
11 SELECT COUNT(ve.EXTRACT('/*').GetStringval()) INTO CountNode
12 FROM TABLE(xmlSequence(EXTRACT(x,'/DATA/LINE'))) ve;
13 dbms_Output.Put_Line('No. of nodes: '
14 ||CountNode);
15 FOR i IN 1..CountNode LOOP
16 dbms_Output.Put_Line(i
17 ||': '
18 ||x.EXTRACT('/DATA/LINE['
19 ||i
20 ||']').GetStringval());
21 END LOOP;
22 END;
23 /
No. of nodes: 4
1: <LINE>A</LINE>
2: <LINE>B</LINE>
3: <LINE>C</LINE>
4: <LINE>D</LINE>
PL/SQL procedure successfully completed.
|
|
|
Re: Number of node elements in XML [message #321169 is a reply to message #321153] |
Mon, 19 May 2008 05:45 |
|
Michel Cadot
Messages: 68728 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Or in SQL:
SQL> select decode(rownum, 1, 'No. of nodes: '||count(*) over ()||'
2 ') || rownum || ': ' || column_value val
3 from table(xmlsequence(extract(xmltype(
4 '<DATA>
5 <LINE>A</LINE>
6 <LINE>B</LINE>
7 <LINE>C</LINE>
8 <LINE>D</LINE>
9 </DATA>'),
10 '/DATA/LINE'))) t
11 /
VAL
-------------------------------------------------------
No. of nodes: 4
1: <LINE>A</LINE>
2: <LINE>B</LINE>
3: <LINE>C</LINE>
4: <LINE>D</LINE>
4 rows selected.
Regards
Michel
|
|
|