Oracle 12 XMLElement:如果 value_expr 为空,则无标记

Posted

技术标签:

【中文标题】Oracle 12 XMLElement:如果 value_expr 为空,则无标记【英文标题】:Oracle 12 XMLElement: no tag if value_expr is null 【发布时间】:2020-01-23 06:29:33 【问题描述】:

来自德国的早安。

现在,如果 value_expr 为空,有没有办法抑制 XMLElement 中的整个标记?

点赞:取消“宾果”标签:

SELECT XMLELEMENT( "artno", trim(artno) )
     , XMLELEMENT( "bingo", NULL )
  FROM articlemasterdata
 WHERE artno = '770100C'

<artnr>770100C</artnr>
<bingo></bingo>

即使是 XMLTYPE 也提供了一个空标签:

SELECT XMLTYPE( XMLELEMENT( "artno", trim(artno) ).getclobval() ) 
    ,  XMLTYPE( XMLELEMENT( "bingo", null ).getclobval() )
  FROM articlemasterdata 
 WHERE artno = '770100C';

<artnr>770100C</artnr>
<bingo></bingo>

谢谢,

约阿希姆

【问题讨论】:

【参考方案1】:

您可以使用xmlforest 代替xmlelement

-- CTE for dummy table
with articlemasterdata (artno) as (select '770100C' from dual)
-- actual query
SELECT XMLELEMENT( "artno", trim(artno) )
     , XMLFOREST(NULL as "bingo")
  FROM articlemasterdata
 WHERE artno = '770100C';

XMLELEMENT("ARTNO",TRIM(ARTNO))                                                  XMLFOREST(NULLAS"BINGO")                                                        
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
<artno>770100C</artno>                                                                                                                                           

或者更现实一点:

-- CTE for dummy table
with articlemasterdata (artno, bingo) as (
  select '770100A', 'legs eleven' from dual
  union all
  select '770100C', null from dual
)
-- actual query
SELECT XMLELEMENT( "artno", trim(artno) )
     , XMLFOREST( bingo as "bingo")
  FROM articlemasterdata
 WHERE artno = '770100A'
    OR artno = '770100C';

XMLELEMENT("ARTNO",TRIM(ARTNO))                                                  XMLFOREST(BINGOAS"BINGO")                                                       
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
<artno>770100A</artno>                                                           <bingo>legs eleven</bingo>                                                      
<artno>770100C</artno>                                                                                                                                           

甚至:

SELECT XMLELEMENT( "root",
         XMLELEMENT( "artno", trim(artno) )
       , XMLFOREST( bingo as "bingo")
       )
  FROM articlemasterdata
 WHERE artno = '770100A'
    OR artno = '770100C';

XMLELEMENT("ROOT",XMLELEMENT("ARTNO",TRIM(ARTNO)),XMLFOREST(BINGOAS"BINGO"))    
--------------------------------------------------------------------------------
<root><artno>770100A</artno><bingo>legs eleven</bingo></root>
<root><artno>770100C</artno></root>

【讨论】:

以上是关于Oracle 12 XMLElement:如果 value_expr 为空,则无标记的主要内容,如果未能解决你的问题,请参考以下文章

oracle xml操作

使用 XMLELEMENT Oracle 时如何替换 ' 或任何特殊字符

如何删除 XmlElement ? (XmlNode 与 XmlElement)

Oracle 在 IN 子句中使用子查询降低性能

JAXB的@XmlElement注解

sh 在MacOSX上安装Oracle即时客户端(sqlplus)v12.1