如何使用XMLAGG

时间:2016-01-31 13:41:31

标签: xml oracle oracle-xml-db

我想为表跟随列聚合多个XMLELEMENT并返回一个xml文件。

以下是表格数据: enter image description here

我希望以下列格式返回xml:

<employee>
<id>FMCSC00015</id>
<year>2016</year>
<month>1</month>
<head_name>BASIC PAY</head_name>
<amount>35600</amount>
<head_name>BANK LOAN-4</head_name>
<amount>23490</amount>
<head_name>RESEARCH ALLOWANCE</head_name>
<amount>1500</amount>
<head_name>MOTOR GARAGE</head_name>
<amount>500.5</amount>
<head_name>CLUB</head_name>
<amount>207</amount>
.........so on            
<employee>

但是我在聚合2列[head_name和amount]时面临困难。 这是我的oracle代码:

 select xmlElement(  "employee",
                      xmlelement("id", e.PAYMSTR_EMPID),
                      xmlelement("year", e.PAYMSTR_SALYR),
                      xmlelement("month", e.PAYMSTR_SALMT),
                      XMLAGG(
                        XMLELEMENT(" ",
                        XMLELEMENT("head_name", e.PAYMSTR_SALHDNM ),
                        XMLELEMENT("amount", e.PAYMSTR_AMOUNT )  ) 
                      )
                  ) as result 
                  from TBL_PAYROLL_MASTER_FILE e                
                  where e.PAYMSTR_EMPID = 'FMCSC00015'
                  group by e.PAYMSTR_EMPID,e.PAYMSTR_SALYR, e.PAYMSTR_SALMT;

正如您所看到的,因为我在XMLAGG的第一个XMLELEMENT中使用了空格,它仍然使用以下输出xml创建空标记

<employee>
    <id>FMCSC00015</id>
    <year>2016</year>
    <month>1</month>
    <>
    <head_name>BASIC PAY</head_name>
    <amount>35600</amount>
    </>
    <>
    <head_name>BANK LOAN-4</head_name>
    <amount>23490</amount>
    </>
    <>
    <head_name>RESEARCH ALLOWANCE</head_name>
    <amount>1500</amount>
    </>
    <>    
    <head_name>MOTOR GARAGE</head_name>
    <amount>500.5</amount>
    </>
    <>
    <head_name>CLUB</head_name>
    <amount>207</amount>
    .........so on            
    <employee>

如何避免使用这些额外的空标签并获取适当的xml格式。谢谢

1 个答案:

答案 0 :(得分:1)

我应该专注于XMLFOREST这样做。

此查询提供所需的XML。

select   xmlElement(  "employee",
              xmlelement("id", e.PAYMSTR_EMPID),
              xmlelement("year", e.PAYMSTR_SALYR),
              xmlelement("month", e.PAYMSTR_SALMT),
              (select   XMLAGG(  
                 xmlforest(PAYMSTR_SALHDNM, PAYMSTR_AMOUNT  ) )
               from my_tab s where  s.PAYMSTR_EMPID = e.PAYMSTR_EMPID and 
                  s.PAYMSTR_SALYR = e.PAYMSTR_SALYR and s.PAYMSTR_SALMT = e.PAYMSTR_SALMT)      
                      )
from  
   ( select distinct PAYMSTR_EMPID, PAYMSTR_SALYR, PAYMSTR_SALMT 
     from  my_tab where  PAYMSTR_EMPID = 'FMCSC00015' ) e;

请注意,由于您的数据是非规范化的,因此我首先选择员工,年和月,然后在子查询中选择所有员工行(提取年份和月份)。

<employee>
  <id>FMCSC00015</id>
  <year>2016</year>
  <month>1</month>
  <PAYMSTR_SALHDNM>BASIC PAY</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>35600</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>BANK LOAN-4</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>23490</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>RESEARCH ALLOWANCE</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>1500</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>MOTOR GARAGE</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>500,5</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>CLUB</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>207</PAYMSTR_AMOUNT>
</employee>

如果您的员工有更多记录(不同的年份和月份,您会为每个记录获得一个XML行

<employee>
  <id>FMCSC00015</id>
  <year>2015</year>
  <month>1</month>
  <PAYMSTR_SALHDNM>CLUB</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>207</PAYMSTR_AMOUNT>
</employee>

我的测试数据

create table my_tab as 
select 'FMCSC00015' PAYMSTR_EMPID, 2016 PAYMSTR_SALYR, 1 PAYMSTR_SALMT, 'BASIC PAY' PAYMSTR_SALHDNM, 35600 PAYMSTR_AMOUNT from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'BANK LOAN-4' head_name, 23490 amount from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'RESEARCH ALLOWANCE' head_name, 1500 amount from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'MOTOR GARAGE' head_name, 500.5 amount from dual union all
select 'FMCSC00015' id, 2016 year, 1 month, 'CLUB' head_name, 207 amount from dual union all
select 'FMCSC00015' id, 2015 year, 1 month, 'CLUB' head_name, 207 amount from dual union all
select 'FMCSC00016' id, 2016 year, 1 month, 'CLUB' head_name, 207 amount from dual;