Skip to Main Content

Database Software

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to count no of nodes under each parent in any given xml

user7955917Aug 24 2012 — edited Aug 27 2012
how to count no of nodes under each parent in any given xml . for example the below xml has
books has 3 childern , bookstore[1] has 4 , bookstore[2] has 6 and bookstore[3] has 2 . is it possible to get count of duplicate tags in a childnode ie bookstore[1]
has book1 tage repeated twice .. vice versa . do we need to go plsql are we can achieve it via sql

<books>
<bookstore>
<book1>abc</book1>
<book2>bca</book2>
<book1>cab</book1>
<book4>bac</book4>
</bookstore>
<bookstore>
<book1>abc</book1>
<book2>bca</book2>
<book3>cab</book3>
<book3>cab</book3>
<bookn_1>bac</bookn_1>
<bookn>acb</bookn>
</bookstore>
<bookstore>
<book1>abc</book1>
<book2>bca</book2>
</bookstore>
</books>


i have tried this ... query .

select
xmlquery('count($doc/books/bookstore[1]/descendant::*)' passing xmltype('<books>
<bookstore>
<book1>abc</book1>
<book1>cab</book1>
<book4>bac</book4>
</bookstore>
<bookstore>
<book1>abc</book1>
<book2>bca</book2>
</bookstore>
</books>')
as "doc" returning content).getNumberVal() as node_count
from dual

select
xmlquery('count($doc/books/descendant::*)' passing xmltype('<books>
<bookstore>
<book1>abc</book1>
<book1>cab</book1>
<book4>bac</book4>
</bookstore>
<bookstore>
<book1>abc</book1>
<book2>bca</book2>
</bookstore>
</books>')
as "doc" returning content).getNumberVal() as node_count
from dual

how can i get the counts for each parent in one single query

Edited by: user7955917 on Aug 24, 2012 7:26 AM
This post has been answered by odie_63 on Aug 25 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2012
Added on Aug 24 2012
5 comments
5,441 views