简体   繁体   English

从 Oracle CLOB 数据库列中获取 XML 值

[英]Getting XML values from a Oracle CLOB database column

By using either PL/SQL or SQL I'm trying to extract specific XML values from a database CLOB column.通过使用 PL/SQL 或 SQL,我试图从数据库 CLOB 列中提取特定的 XML 值。

Table : PDI_SUBMITTED_XML
(PSX_AGREEMENT  NUMBER(10),
 PSX_DOCUMENT   CLOB)

For example I'm trying to extract the value " Broker Region " from the BranchName tag below from the actual CLOB contents.例如,我试图从实际 CLOB 内容中从下面的BranchName标记中提取值“ Broker Region ”。

<?xml version="1.0" encoding="UTF-8"?>
<tns:AgreementWrapper xmlns:tns="http://ws.pancredit.com/wsdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <tns:Agreement>
    <tns:AdminFee>199</tns:AdminFee>
    <tns:AdminFeeFinanced>true</tns:AdminFeeFinanced>
    <tns:Affordability>
      <tns:DownturnReason/>
    </tns:Affordability>
    <tns:AgreementNumber>13498443</tns:AgreementNumber>
    <tns:BankAccountAlreadyValidated>false</tns:BankAccountAlreadyValidated>
    <tns:BankAccountNumber>70872490</tns:BankAccountNumber>
    <tns:BankSortCode>404784</tns:BankSortCode>
    <tns:BranchName>Broker Region</tns:BranchName>
    <tns:BrandName>Rtl - VAT Assist Brand</tns:BrandName>

Up to now I've found no solution that will do this, someone has mentioned XMLPATH but see not many examples on internet that does it for the above example.到目前为止,我还没有找到可以做到这一点的解决方案,有人提到了 XMLPATH,但在互联网上看到的例子并不多,上面的例子就是这样做的。

Does anybody have a solution to what seems simple enough to do but I've had no success up to now.有没有人可以解决看起来很简单的事情,但到目前为止我还没有成功。

You can use XMLQuery to get a single value:您可以使用 XMLQuery 获取单个值:

select xmlquery(
  'declare namespace tns="http://ws.pancredit.com/wsdl";
  /tns:AgreementWrapper/tns:Agreement/tns:BranchName/text()'
  passing xmltype(PSX_DOCUMENT)
  returning content
  ).getstringval() as branch_name
from PDI_SUBMITTED_XML
BRANCH_NAME分店名称
Broker Region经纪人地区

Or XMLTable if you need to get several things at once:或者 XMLTable 如果您需要一次获得几件事情:

select x.agreement_number, x.branch_name
from PDI_SUBMITTED_XML
cross apply xmltable(
  xmlnamespaces('http://ws.pancredit.com/wsdl' as "tns"),
  '/tns:AgreementWrapper/tns:Agreement'
  passing xmltype(PSX_DOCUMENT)
  columns
    agreement_number number path 'tns:AgreementNumber',
    branch_name varchar2(30) path 'tns:BranchName'
) x
AGREEMENT_NUMBER AGREEMENT_NUMBER BRANCH_NAME分店名称
13498443 13498443 Broker Region经纪人地区

In both cases the tns namespace has to be declared (unless you wildcard it).在这两种情况下,都必须声明tns命名空间(除非您使用通配符)。

db<>fiddle db<>小提琴

Read more about those functions in the documentation. 在文档中阅读有关这些功能的更多信息。

Adding to Alex Poole's answer one more alternative.为亚历克斯普尔的答案添加了另一种选择。 The DB fiddle here 数据库小提琴在这里

    SELECT 
extractvalue(xmltype(PSX_DOCUMENT),'/AgreementWrapper/Agreement/BranchName',
    'xmlns="http://ws.pancredit.com/wsdl"') as Branch_name
    from PDI_SUBMITTED_XML;

For multiple values, you can use multiple extractvalues.对于多个值,您可以使用多个提取值。 The DB Fiddle here DB Fiddle在这里

    SELECT 
      extractvalue(
        xmltype(PSX_DOCUMENT), 
        '/AgreementWrapper/Agreement/BranchName', 
        'xmlns="http://ws.pancredit.com/wsdl"'
      ) as BRanch_name, 
      extractvalue(
        xmltype(PSX_DOCUMENT), 
        '/AgreementWrapper/Agreement/AgreementNumber', 
        'xmlns="http://ws.pancredit.com/wsdl"'
      ) as AgreementNumber 
    from 
      PDI_SUBMITTED_XML;

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

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