基于XML Clob搜索字符串从oracle提取行

时间:2019-02-13 17:11:41

标签: xml database oracle

我们正在使用一种将审计信息存储在XML Clob中的oracle数据库中的产品。下面是xml格式

<Attributes>
  <Map>
    <entry key="messages">
      <value>
        <List>
          <Message key="err_exception" type="Error">
            <Parameters>
              <Message key="sailpoint.tools.GeneralException: The application script threw an exception: java.lang.Exception: This request could not be approved as you are the only available approval workgroup member; per CIS policy, a user is not allowed to approve their own request!! BSF info: script at line: 0 column: columnNo" type="Error"/>
            </Parameters>
          </Message>
        </List>
      </value>
    </entry>
  </Map>
</Attributes>

我正在使用以下查询来获取值

select * from (select x.*
from identityiq.SPT_IDENTITY_REQUEST att,
      xmltable('Attributes'
      passing xmltype(att.attributes)
      columns Message varchar2(200) path '/Attributes/Map/entry[@key="messages"]/value/List/Message[@key="err_exception"]/Parameters/Message/@key' ) x
      where to_date('01/01/1970 00:00:00','mm-dd-yyyy HH24:MI:SS')+(att.created-14400000)/1000/60/60/24 <= to_date('10-01-2019 00:00:00', 'dd-mm-yyyy HH24:MI:SS') and
      to_date('01/01/1970 00:00:00','mm-dd-yyyy HH24:MI:SS')+(att.created-14400000)/1000/60/60/24 >= to_date('01-01-2019 00:00:00', 'dd-mm-yyyy HH24:MI:SS')) res
      where res.message is not null 
      and res.message like '%CIS policy%';

但是我遇到以下错误

ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
19279. 00000 -  "XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence" 
*Cause:    The XQuery sequence passed in had more than one item.
*Action:   Correct the XQuery expression to return a single item sequence.

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您的xml数据实际上与您提供给我们的xml格式不匹配。 :)

XPath表达式中的一个级别返回2个节点而不是1个。我猜它是<List>节点,具有多个<Message>子节点,但可以是任何子节点。解决问题的方法如下:

-- sample data
with SPT_IDENTITY_REQUEST as (select to_clob('<Attributes>
  <Map>
    <entry key="messages">
      <value>
        <List>
          <Message key="err_exception" type="Error">
            <Parameters>
              <Message key="CIS policy sample exception #1" type="Error"/>
            </Parameters>
          </Message>
          <Message key="err_exception" type="Error">
            <Parameters>
              <Message key="CIS policy sample exception #2" type="Error"/>
            </Parameters>
          </Message>
        </List>
      </value>
    </entry>
  </Map>
</Attributes>') as attributes from dual)
-- your query
select * from (select *
from identityiq.SPT_IDENTITY_REQUEST att,
      xmltable('Attributes'
        passing xmltype(att.attributes)
        columns MessageXML XMLType path '/Attributes/Map/entry[@key="messages"]/value/List/Message[@key="err_exception"]' ) x,
      xmltable('/Message' -- add a second xmltable to handle multiple nodes at this level
        passing x.MessageXML
        columns Message varchar2(100) path '/Message/Parameters/Message/@key') m
      ) res
      where res.message is not null 
      and res.message like '%CIS policy%';

另请参见this answer to a similar question,其中更深入地介绍了它。