XML to XML transformation for attribute based response using XSLT

Viewed 35

I have input xml request and i need to transform it another xml format using xslt. the point here is if i have RouteViaPoint in request more than one i need to create AssociatedFlightLegSchedule in response xml. let say if i have 2 RouteViaPoints I need to map in arrival airport with sequence number 1 and in departure airport with sequence number 0. if i have 3 RouteViaPoints I need to map in arrival airport with sequence number 1 and in departure airport with sequence number 0. and also need to map in arrival airport value with sequence number 2 and departure airport 1.

My input request:-

<Content>
                <FlightUpdatedNotification>
                    <Flight>`enter code here`
                        <FlightState>
                            <ScheduledTime>2022-03-14T14:15:00</ScheduledTime>
                            <Aircraft>
                                <AircraftId>
                                    <Registration>ABCD</Registration>
                                </AircraftId>
                                <Value propertyName="IsRetired">false</Value>
                            </Aircraft>
                            <Route customsType="International">
                                <ViaPoints>
                                    <RouteViaPoint sequenceNumber="0">
                                        <AirportCode codeContext="IATA">KUL</AirportCode>
                                        <AirportCode codeContext="ICAO">WMKK</AirportCode>
                                    </RouteViaPoint>
                                    <RouteViaPoint sequenceNumber="1">
                                        <AirportCode codeContext="IATA">WMKK</AirportCode>
                                        <AirportCode codeContext="ICAO">BKK</AirportCode>
                                    </RouteViaPoint>
                                    <RouteViaPoint sequenceNumber="2">
                                        <AirportCode codeContext="IATA">BKK</AirportCode>
                                        <AirportCode codeContext="ICAO">SYD</AirportCode>
                                    </RouteViaPoint>
                                </ViaPoints>
                            </Route>
                        </FlightState>
                    </Flight>
                </FlightUpdatedNotification>
            </Content>
       </Envelope>

Output format:-

<?xml version="1.0" encoding="UTF-8"?>
        <FlightLegNotifRsp>
            <Originator CompanyShortName="FCS2"/>
            <FlightLeg>
                    <!--Route if have more than 1-->
                    <AssociatedFlightLegSchedule>
                        <DepartureAirport CodeContext="3">KUL</DepartureAirport>
                        <ArrivalAirport CodeContext="3">WMKK</ArrivalAirport>
                    </AssociatedFlightLegSchedule>
                     <AssociatedFlightLegSchedule>
                        <DepartureAirport CodeContext="3">WMKK</DepartureAirport>
                        <ArrivalAirport CodeContext="3">BKK</ArrivalAirport>
                    </AssociatedFlightLegSchedule>
                </LegData>
                
            </FlightLeg>
        </FlightLegNotifRsp>

XSLT i tried is

<xsl:choose>
                    <xsl:when test="count(ns2:Envelope/ns2:Content/ns2:FlightUpdatedNotification/ns2:Flight/ns1:FlightState/ns1:Route/ns1:ViaPoints/ns1:RouteViaPoint)>=1">
                        <xsl:for-each select="ns2:Envelope/ns2:Content/ns2:FlightUpdatedNotification/ns2:Flight/ns1:FlightState/ns1:Route/ns1:ViaPoints/ns1:RouteViaPoint">
                            <xsl:variable name="var" select="position()"/>
                            <xsl:variable name="var3" select="count(ns2:Envelope/ns2:Content/ns2:FlightUpdatedNotification/ns2:Flight/ns1:FlightState/ns1:Route/ns1:ViaPoints/ns1:RouteViaPoint)"/>
                            <xsl:choose>
                                <xsl:when test="($var)&lt;($var3)">
                                    <ns:AssociatedFlightLegSchedule>
                                        <xsl:variable name="var1" select="$var - 1"/>
                                        <xsl:attribute name="RepeatIndex">
                                            <xsl:value-of select="$var3"/>
                                        </xsl:attribute>
                                        <ns:DepartureAirport>
                                            <xsl:attribute name="CodeContext">
                                                <xsl:value-of select="'3'"/>
                                            </xsl:attribute>
                                            <xsl:value-of select="ns2:Envelope/ns2:Content/ns2:FlightUpdatedNotification/ns2:Flight/ns1:FlightState/ns1:Route/ns1:ViaPoints/ns1:RouteViaPoint[@sequenceNumber=0]/ns1:AirportCode[@codeContext='IATA']"/>
                                        </ns:DepartureAirport>
                                        <ns:ArrivalAirport>
                                            <xsl:attribute name="CodeContext">
                                                <xsl:value-of select="'3'"/>
                                            </xsl:attribute>
                                            <xsl:value-of select="ns2:Envelope/ns2:Content/ns2:FlightUpdatedNotification/ns2:Flight/ns1:FlightState/ns1:Route/ns1:ViaPoints/ns1:RouteViaPoint[@sequenceNumber=1]/ns1:AirportCode[@codeContext='IATA']"/>
                                        </ns:ArrivalAirport>
                                    </ns:AssociatedFlightLegSchedule>
                                </xsl:when>
                            </xsl:choose>
                        </xsl:for-each>
                    </xsl:when>
                </xsl:choose>
0 Answers
Related