ORA-19279 XPTY0004 - Xquery动态类型不匹配:预期的单例序列 - 得到多项序列

时间:2016-04-28 14:41:02

标签: oracle11g oracle-sqldeveloper xml-namespaces xmltable

我在CLOB列中有下面的xml消息:

<Message type="Close Subscription" creationdatetime="15/01/2010 07:48" market="01" xmlns="http://test.org/">
  <Customer userId="data" market="01">
    <UserAccount>test@hotmail.com</UserAccount>
    <ExpireDate>15/02/2016 07:48:11</ExpireDate>
    <Member>
      <MemberReferency number="000003" digit="85" market="01" name="John Rambo"/>
    </Member>
    <Email Id="700">
      <Address>test@hotmail.com</Address>
      <IsConfirmed>True</IsConfirmed>
      <Privacies>
        <Privacy type="C" value="I"/>
        <Privacy type="M" value="I"/>
      </Privacies>
    </Email>
    <Newsletter mailService="NL">
      <language>N</language>
      <frequency>0</frequency>
      <promotion/>
      <origin/>
    </Newsletter>
  </Customer>
</Message>

对于此xml邮件,我有两倍的xml标记隐私

  <Privacies>
    <Privacy type="C" value="I"/>
    <Privacy type="M" value="T"/>
  </Privacies>

如果没有获取错误,我怎样才能获得其中一个隐私的价值?

我的意思是,要获得类型的隐私=&#39; C&#39;。

现在我使用下面的查询包含错误:

select Privacy
  from (select *
          from ta_gen.notification_log a,
               XMLTable(XMLNAMESPACES('http://test.org/' AS "XML"),
                        '/XML:Message' passing xmltype(NTL_MSG) columns                            
                        Privacy VARCHAR2(1)     path 'XML:Customer/XML:Email/XML:Privacies/XML:Privacy/@value') O
         where a.ntl_type = 'Message')

注意:当xml标记隐私在xml邮件中一次时,不会引发错误。

1 个答案:

答案 0 :(得分:2)

根据您只有M型节点时想要发生的事情,您可以显式搜索类型属性为C:

select x.privacy
from notification_log a
cross join XMLTable(XMLNamespaces('http://test.org/' AS "XML"),
  '/XML:Message'
  passing XMLType(ntl_msg)
  columns privacy varchar2(1)
    path 'XML:Customer/XML:Email/XML:Privacies/XML:Privacy[@type="C"]/@value') x
where a.ntl_type = 'Message';

PRIVACY
-------
I

或者您可以通过第二级XMLTable提取所有隐私节点的类型和值,然后决定保留哪些:

select min(x2.privacy) keep (dense_rank first order by x2.type) as privacy
from notification_log a
cross join XMLTable(XMLNamespaces('http://test.org/' AS "XML"),
  '/XML:Message' passing XMLType(ntl_msg)
  columns privacies XMLType path 'XML:Customer/XML:Email/XML:Privacies') x1
cross join XMLTable(XMLNAMESPACES('http://test.org/' AS "XML"),
  'XML:Privacies/XML:Privacy'
  passing privacies
  columns type varchar2(1) path '@type',
    privacy varchar2(1) path '@value') x2
where a.ntl_type = 'Message';

PRIVACY
-------
I      

如果您运行相同的查询,而是select x2.type, x2.value from ...,那么您会看到:

TYPE PRIVACY
---- -------
C    I      
M    I      

..并且min(x2.privacy) keep (dense_rank first order by x2.type)从'lower'(根据字符串比较)类型值获取值类型;这意味着如果两者都存在,它优先于C而不是M,但如果只有一个

则使用