count node in xmltype data [message #343275] |
Tue, 26 August 2008 17:12 |
casttree
Messages: 83 Registered: August 2008
|
Member |
|
|
I need to count the number of node in a varchar2 variable.
The xml data like below, I hope to count how many node test_id in the xml file. In this xml data, we have 2 test_id (as 5 and 51). so I need to get 2 as result in a PL/SQL package.
I looked extract and extractvalue
<report db_version="11.2.0.0.1">
<report_id><![CDATA[/orarep/sqltune/auto_summary?section=task_stats]]></report_id>
<test_id>5</test_id>
<test_id>51</tets_id>
</report>
The blow query returns 551 as result, Do you have any suggestions to sepearte the data and get the count for the node number?
select nvl(XMLType(:rept).extract('//test_id/').getstringval(), '(NULL)') into :te from dual;
Thanks,
|
|
|
|
Re: count node in xmltype data [message #343284 is a reply to message #343279] |
Tue, 26 August 2008 18:21 |
casttree
Messages: 83 Registered: August 2008
|
Member |
|
|
Yes, I need to know how many tag for test_id , so the above case could be 3 and 2 respectively.
I tried the following query, looks it is working, but I am not 100% sure if there is bug or better query to do it?
(Note: rept is the variable with xml data)
Quote: | select count(*) into :te from table(xmlsequence(XMLType(:rept).extract('//test_id'))) t;
|
|
|
|
|
Re: count node in xmltype data [message #343316 is a reply to message #343292] |
Tue, 26 August 2008 23:08 |
casttree
Messages: 83 Registered: August 2008
|
Member |
|
|
Yeah, I think it is a good method for the issue. My data is not much long. maybe 1K at most. And all test_id should be the tag name, but not data.
I try to compare the time spend for two ways by set timing on, the result is random and can't distiguish it obviously.
It looks both way are working,
Quote: | length(:rept>)-length(replace(:rept,'<test_id>')))/length('<test_id>')
|
Quote: | select count(*) into :te from table(xmlsequence(XMLType(:rept).extract('//test_id'))) t
|
Looks the way of length is more simple, is that only reason we should use the length way? (I feel it is not strong enough )
Is there other reasons that one method is better than another one?
Thanks,
|
|
|
|
|