检查 XML 中的值

发布于 2024-11-29 13:45:11 字数 1111 浏览 2 评论 0原文

我有变量

LRowNode DBMS_XMLDOM.DOMNode;

包含(示例)

<N_4883636>
  <TID_23787542>12</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>10</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>

<N_4883636>
  <TID_23787542>0</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>0</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>

如何检查至少一个非空且不为空

I have variable

LRowNode DBMS_XMLDOM.DOMNode;

contains (example)

<N_4883636>
  <TID_23787542>12</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>10</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>

or

<N_4883636>
  <TID_23787542>0</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>0</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>

how to check whether the at least one non-empty and not null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居者 2024-12-06 13:45:11

这只是一个基本示例,可能不适合您,但应该让您了解如何使用 DBMS_XMLDOM。

另一种选择是使用 DBMS_XMLDOM.GETXMLTYPE 来更改游戏规则,使用所有这些 XMLTYPE 好东西。

create or replace function has_data (p_node in dbms_xmldom.domnode)
return boolean as
  subnodes dbms_xmldom.domnodelist;
  node dbms_xmldom.domnode;
  strval varchar2(32767);
  val number;
begin
  subnodes := dbms_xmldom.getchildnodes(p_node);

  dbms_output.put_line('number of subnodes: ' || dbms_xmldom.getlength(subnodes));

  for i in 0 .. dbms_xmldom.getlength(subnodes) - 1 loop
    node := dbms_xmldom.item(subnodes, i);
    strval := dbms_xmldom.getnodevalue(dbms_xmldom.getfirstchild(node));
    dbms_output.put_line(
      '(' || i || ') ' ||
      dbms_xmldom.gettagname(dbms_xmldom.makeelement(node)) ||
      ' = ' || strval
    );

    if strval is not null then
      val := to_number(strval);
      if val > 0 then
        dbms_output.put_line('Node has data.');
        return true;
      end if;
    end if;

  end loop;

  dbms_output.put_line('Node has no data.');
  return false;
end;
/
show errors

declare
  dataset1 constant varchar2(32767) :=
'<N_4883636>
  <TID_23787542>12</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>10</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>';

  dataset2 constant varchar2(32767) :=
'<N_4883636>
  <TID_23787542>0</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>0</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>';

  dataset3 constant varchar2(32767) :=
'<N_4883636>
  <TID_23787542>0</TID_23787542>
  <TID_23787543>0</TID_23787543>
  <TID_23787533>3</TID_23787533>
</N_4883636>';

  doc dbms_xmldom.domdocument;
  nodes dbms_xmldom.domnodelist;
  x boolean;
begin
  doc := dbms_xmldom.newdomdocument(dataset1);
  nodes := dbms_xmldom.getelementsbytagname(doc, 'N_4883636');
  x := has_data(dbms_xmldom.item(nodes, 0));

  doc := dbms_xmldom.newdomdocument(dataset2);
  nodes := dbms_xmldom.getelementsbytagname(doc, 'N_4883636');
  x := has_data(dbms_xmldom.item(nodes, 0));

  doc := dbms_xmldom.newdomdocument(dataset3);
  nodes := dbms_xmldom.getelementsbytagname(doc, 'N_4883636');
  x := has_data(dbms_xmldom.item(nodes, 0));
end;
/

This is just a basic example and might not fit for you as such but should give you an idea how to use DBMS_XMLDOM.

Another alternative is to use DBMS_XMLDOM.GETXMLTYPE to change the rules of the game use all those XMLTYPE goodies.

create or replace function has_data (p_node in dbms_xmldom.domnode)
return boolean as
  subnodes dbms_xmldom.domnodelist;
  node dbms_xmldom.domnode;
  strval varchar2(32767);
  val number;
begin
  subnodes := dbms_xmldom.getchildnodes(p_node);

  dbms_output.put_line('number of subnodes: ' || dbms_xmldom.getlength(subnodes));

  for i in 0 .. dbms_xmldom.getlength(subnodes) - 1 loop
    node := dbms_xmldom.item(subnodes, i);
    strval := dbms_xmldom.getnodevalue(dbms_xmldom.getfirstchild(node));
    dbms_output.put_line(
      '(' || i || ') ' ||
      dbms_xmldom.gettagname(dbms_xmldom.makeelement(node)) ||
      ' = ' || strval
    );

    if strval is not null then
      val := to_number(strval);
      if val > 0 then
        dbms_output.put_line('Node has data.');
        return true;
      end if;
    end if;

  end loop;

  dbms_output.put_line('Node has no data.');
  return false;
end;
/
show errors

declare
  dataset1 constant varchar2(32767) :=
'<N_4883636>
  <TID_23787542>12</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>10</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>';

  dataset2 constant varchar2(32767) :=
'<N_4883636>
  <TID_23787542>0</TID_23787542>
  <TID_23787543></TID_23787543>
  <TID_23787532/>
  <TID_23787533>0</TID_23787533>
  <TID_23787534/>
  <TID_23787535/>
  <TID_23787536>0</TID_23787536>
  <TID_23787537/>
  <TID_23787538/>
  <TID_23787539>0</TID_23787539>
  <TID_23787540>0</TID_23787540>
  <TID_23787541>0</TID_23787541>
</N_4883636>';

  dataset3 constant varchar2(32767) :=
'<N_4883636>
  <TID_23787542>0</TID_23787542>
  <TID_23787543>0</TID_23787543>
  <TID_23787533>3</TID_23787533>
</N_4883636>';

  doc dbms_xmldom.domdocument;
  nodes dbms_xmldom.domnodelist;
  x boolean;
begin
  doc := dbms_xmldom.newdomdocument(dataset1);
  nodes := dbms_xmldom.getelementsbytagname(doc, 'N_4883636');
  x := has_data(dbms_xmldom.item(nodes, 0));

  doc := dbms_xmldom.newdomdocument(dataset2);
  nodes := dbms_xmldom.getelementsbytagname(doc, 'N_4883636');
  x := has_data(dbms_xmldom.item(nodes, 0));

  doc := dbms_xmldom.newdomdocument(dataset3);
  nodes := dbms_xmldom.getelementsbytagname(doc, 'N_4883636');
  x := has_data(dbms_xmldom.item(nodes, 0));
end;
/
如梦初醒的夏天 2024-12-06 13:45:11
LNonEmptyIndexes := DBMS_XSLPROCESSOR.SELECTNODES(LRowNode, './*[text()!=''0'']');
nNON_EMPTY_COUNT := DBMS_XMLDOM.getLength(LNonEmptyIndexes);
IF (nNON_EMPTY_COUNT != 0) THEN
...
END IF;
LNonEmptyIndexes := DBMS_XSLPROCESSOR.SELECTNODES(LRowNode, './*[text()!=''0'']');
nNON_EMPTY_COUNT := DBMS_XMLDOM.getLength(LNonEmptyIndexes);
IF (nNON_EMPTY_COUNT != 0) THEN
...
END IF;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文