oracle xml操作

Posted 田灬禾-不忘初心,方得始終

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle xml操作相关的知识,希望对你有一定的参考价值。

--xmlelement多个标签层级
SELECT XMLELEMENT("TEST",
                  XMLELEMENT("AA",
                             XMLELEMENT("BB", XXX),
                             XMLELEMENT("CC", XXX)))
  FROM DUAL;
  SELECT XMLELEMENT("test")
    FROM DUAL
----XMLATTRIBUTES 多个属性
SELECT XMLELEMENT("TEST",
                  123,
                  XMLELEMENT("AA",
                             XMLATTRIBUTES(1235678 "test_attribute"),
                             XMLELEMENT("BB",
                                        XMLATTRIBUTES(z "attr",NULL "XO"),
                                        XXX),
                             XMLELEMENT("CC", XXX)),
                  qwe)
  FROM DUAL;
  
--xmlforest ,如果要定义标签节点属性值则不能用
SELECT XMLELEMENT("TEST",XMLFOREST(NULL "WW",1 "XX",3 "xx")) FROM dual;
--关于空值是否会生成生应的标签
--1、xmlelement 空值或null都会有结束的标签不会出现</> ,xmlattributes 空值或null对应的键名不会出现
    SELECT XMLELEMENT("test", XMLELEMENT("test1", XMLATTRIBUTES(1 "test1_attribute"),NULL),
                              XMLELEMENT("test2", XMLATTRIBUTES(‘‘ "test2_attribute"),‘‘),
                              XMLELEMENT("test3", XMLATTRIBUTES(NULL "test3_attribute")),haha)
    FROM dual;
--2.xmlforest  null整个标签名不会出现,单引号会出现
SELECT XMLELEMENT("test", XMLFOREST(NULL "test1",‘‘ test3)) FROM dual;
--xmlagg 生成xml片段
WITH t AS (SELECT 1 a,A b FROM dual)-- UNION ALL SELECT ‘2‘,‘A‘ b FROM dual UNION ALL SELECT ‘3‘,‘B‘ b FROM dual)
--1
SELECT XMLELEMENT("KK",XMLAGG(XMLELEMENT("TEST",a))) FROM t GROUP BY b;
--2
SELECT XMLELEMENT("TT",(SELECT XMLAGG(XMLELEMENT("TEST",a)  ORDER BY a) FROM t)) FROM dual;
--排序
SELECT XMLELEMENT("KK",XMLAGG(XMLELEMENT("TEST",a) ORDER BY a DESC)) FROM t GROUP BY b;
--可生成无效的xml,xml只能有一个根结节点
SELECT XMLAGG(XMLELEMENT("TEST",a)) FROM t
--以下会报错
SELECT XMLTYPE.CreateXML(<dummy>X</dummy><dummy>y</dummy>) FROM dual;

--聚合,可替代 sys_path_connect_by,listagg
WITH Q AS
 (SELECT 1 KEY, A X
    FROM DUAL
  UNION ALL
  SELECT 2 KEY, B X
    FROM DUAL
  UNION ALL
  SELECT 3 KEY, A X
    FROM DUAL
  UNION ALL
  SELECT 4 KEY, A X
    FROM DUAL)
--1    
SELECT X,
RTRIM(
XMLAGG (XMLELEMENT(e, key||,) ORDER BY key)
) AS concatval
FROM q
GROUP BY X;
--2
SELECT X,
       RTRIM(XMLAGG(XMLELEMENT(E, KEY || ,) ORDER BY KEY)
             .EXTRACT(//text()),
             ,) AS CONCATVAL
  FROM Q
 GROUP BY X;

 

以上是关于oracle xml操作的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Toad for Oracle 中使用自定义代码片段?

xml Eclipse模板(代码片段)检查参数并最终抛出IllegalArgumentException

Oracle-常用数据库对象笔记(片段)

需要示例代码片段帮助

在“设置”片段中夸大类PreferenceScreen的错误

从 xml 片段中删除 html 标签? [复制]