使用XMLEXISTS进行查询错误地具有“如果有”行为(Oracle 11g)

时间:2015-11-20 17:07:37

标签: xml plsql oracle11g xmltype

我正在开发一个涉及查询XML文档内容的项目。我想出了一个简化的测试用例(下面)来试着理解它是如何工作的。问题是,即使一条记录具有该标记,haskey字段也适用于每条记录。如何根据每个记录中单个子节点的存在来返回true / false?

DECLARE
  players xmltype := xmltype(' 
<Players>
  <Player username="player1">
    <HasKey></HasKey>
  </Player>
  <Player username="player2">
    <HasKey></HasKey>
  </Player>
  <Player username="player3">
  </Player>
</Players>');
BEGIN
  FOR c IN
  (SELECT username,
    (
    CASE
      WHEN xmlexists('$p/Players/Player/HasKey' passing players AS "p")
      THEN 'TRUE'
      ELSE 'FALSE'
    END) haskey
  FROM xmltable('/Players/Player' passing Players columns username VARCHAR2(30) path '@username' ) "p"
  )
  LOOP
    dbms_output.put_line(c.username ||','|| c.haskey);
  END LOOP;
END;

0 个答案:

没有答案