Subject:Regarding XSLT logic for a simple input XML message Author:amith swamy Date:14 Nov 2007 07:33 AM
Hi,
We are facing below issue in XSL logic. Please help
If input XML has same elements that is repeating twice and should be mapped to the same element in the output XML..
For Ex:
If input XML is,
<?xml version="1.0"?>
<root>
<ReservationControlInformationSegment>
<CompanyId>AAH</CompanyId>
<ReservationControlNumber>ABC456789</ReservationControlNumber>
<CompanyId>AAH</CompanyId>
<ReservationControlNumber>ABC456789</ReservationControlNumber>
<ReservationControlType>B</ReservationControlType>
</ReservationControlInformationSegment>
</root>
In ReservationControlInformationSegment, three elements are there that can repeat any number of times(Consider 2 times). But in the above example for the first time, third element(ReservationControlType) is not present. Second time its present.
The expected output XML is
<root>
<RCI>
<RCI01>
<RCI0101>AAH</RCI0101>
<RCI0102>ABC456789</RCI0102>
</RCI01>
<RCI02>
<RCI0201>AAH</RCI0201>
<RCI0202>ABC456789</RCI0202>
<RCI0203>B</RCI0203>
</RCI02>
</RCI>
</root>
But the O/P is in the following format where extra <RCI0103/> is coming
<?xml version="1.0"?>
<root>
<RCI>
<RCI01>
<RCI0101>AAH</RCI0101>
<RCI0102>ABC456789</RCI0102>
<RCI0103/>
</RCI01>
<RCI02>
<RCI0201>AAH</RCI0201>
<RCI0202>ABC456789</RCI0202>
<RCI0203>B</RCI0203>
</RCI02>
</RCI>
</root>
This is because while checking for the if condition we are checking for the following sibling of CompanyId.
Can you please suggest if there is any better approach to solve this problem?
In the output, in place of ReservationControlNumber, ReservationControlType information will be replaced.
If any information is missing in the middle then empty tag should be there.
But if it is missing at the end, then empty tags need not be there in the output.
For ex:
If four elements should be there in RCI01 and last 2 elements are not present, then empty tags for those 2 elements should not come. But if 2 middle elements are missing then first element should come and middle two elements should come with an empty tag and last element information should be there.