简体   繁体   English

如何在Oracle SQL Developer中提取和求和xml属性值

[英]How to extract and sum xml attribute values in oracle sql developer

I have a lot of large xml messages stored as CLOBs in a column xml_message in table online_message . 我有很多存储在表online_messagexml_message CLOB的大XML消息。 For example the messages looks something like this: 例如,消息看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<CustomerDetails xmlns="http://something.com/version/1.0">
<CustomerId>100345</CustomerId>
<AccountDetails Account="account1" Value="103"/>
<AccountDetails Account="account2" Value="142"/>
<AccountDetails Account="account3" Value="345"/>
<AccountDetails Account="account4" Value="634"/>
</CustomerDetails>

<?xml version="1.0" encoding="utf-8" ?>
<CustomerDetails xmlns="http://something.com/version/1.0">
<CustomerId>100465</CustomerId>
<AccountDetails Account="account5" Value="198"/>
<AccountDetails Account="account6" Value="567"/>
<AccountDetails Account="account7" Value="1984"/>
<AccountDetails Account="account8" Value="84"/>
</CustomerDetails>

I want to get all the Value attributes in all xml messages and sum them up. 我想获取所有xml消息中的所有Value属性并将其汇总。 So the final result (in this example) should be 103+142+345+634+198+567+1984+84=4057. 因此,最终结果(在此示例中)应为103 + 142 + 345 + 634 + 198 + 567 + 1984 + 84 = 4057。 Any idea how i can do this in sql developer? 任何想法我怎么能在sql developer中做到这一点?

Thanks! 谢谢!

You may want to try this: 您可能要尝试以下操作:

SELECT sum(t.v) 
FROM online_message, XMLTABLE('for $i in //*/@Value return $i' 
  PASSING online_message.xml_message 
  COLUMNS v NUMBER PATH '.') t
;

here is a related method - you can try to edit for your purposes. 这是一种相关方法-您可以尝试根据自己的目的进行编辑。

select Sum(
         to_number(
            extractvalue(
               xmltype(
                  dbms_xmlgen.getxml('select count(*) c from '||table_name)),'/ROWSET/ROW/C'))) count
from user_tables;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM