I want to aggregate multiple XMLELEMENT for table following columns and return an xml file.
I want return xml with following format:
<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>
But i am facing difficulty on aggregating 2 columns [head_name and amount]. here is my oracle code:
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;
As you can see because i used white space inside the first XMLELEMENT of XMLAGG, it still creates empty tags with following output 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>
How can i avoid this extra empty tags and get my appropriate xml format.Thanks
I thing you should focus on XMLFOREST it makes the trick.
This query provides a required XML.
Note that because your data are denormalized, I first select distinct the employee, year and month, after that in the subquery all employees rows (for the year and month are fetched).
In case your employee has more records (with different years and months you get one XML row for each of them
My test data