I have a requirement where I need to output optional XML elements always (irrespective of data availability) , while writing into a .csv file.
XML File Format
<entries>
<countryCode>123</countryCode>
<adminDataMap>
<testAccount>True</testAccount>
<code>11</code>
</adminDataMap>
<privateMap>
<email>[email protected]</email>
<cancelReason>Not interested</cancelReason>
</privateMap>
</entries>
Here 'entries' Root element 'cancelReason', 'testAccount' are optional elements which will only available once data is there.
i.e Some records will have those 'optional' fields and others not.
I have written a XQuery to extract all these elements
let $entries := /root/entries
return
for $entry in $entries
return
<entries>
{
$entry/*,
$entry/privateMap/email,
$entry/privateMap/cancelReason,
$entry/adminDataMap/code,
$entry/adminDataMap/testAccount
}
</entries>
My requirement is to extract all mandatory and optional elements as OUTPUT. i.e Output Metadata always remain same. If optional 'cancelReason' doesn't have a value, it will be populated as blanks/nulls [something like that]
Please advise of any options in XQUERY which can be written so as to achieve this.