I am a beginner in XSLT.
Below is source XML which i receive. Request tag constains the FlightId which is being used to filter the Result tag.
Source XML:
<Response>
<Request>
<RequestedFlights>
<FlightId>2121</FlightId>
<FlightId>2584</FlightId>
</RequestedFlights>
</Request>
<Result>
<Flights>
<Flight>
<Segments>
<Segment>
<Id>1</Id>
<FlightNumber>2121</FlightNumber>
</Segment>
<Segment>
<Id>2</Id>
<FlightNumber>1121</FlightNumber>
</Segment>
</Segments>
</Flight>
<Flight>
<Segments>
<Segment>
<Id>3</Id>
<FlightNumber>2121</FlightNumber>
</Segment>
<Segment>
<Id>4</Id>
<FlightNumber>2584</FlightNumber>
</Segment>
</Segments>
</Flight>
<Flight>
<Segments>
<Segment>
<Id>5</Id>
<FlightNumber>2121</FlightNumber>
</Segment>
<Segment>
<Id>6</Id>
<FlightNumber>2584</FlightNumber>
</Segment>
<Segment>
<Id>7</Id>
<FlightNumber>2023</FlightNumber>
</Segment>
</Segments>
</Flight>
</Flights>
</Result>
</Response>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="ReqFlights" select="//Request/RequestedFlights/FlightId" />
<xsl:variable name="FilterFlights" select="//Result/Flights/Flight[Segments/Segment/FlightNumber=$ReqFlights]"/>
<xsl:template match="Response">
<FilterResult>
<ResultCount>
<xsl:value-of select="count($FilterFlights)"/>
</ResultCount>
<xsl:copy>
<xsl:copy-of select="$FilterFlights"/>
</xsl:copy>
</FilterResult>
</xsl:template>
</xsl:stylesheet>
I received below output using above XSLT.
Output:
<FilterResult>
<ResultCount>3</ResultCount>
<Response>
<Flight>
<Segments>
<Segment>
<Id>1</Id>
<FlightNumber>2121</FlightNumber>
</Segment>
<Segment>
<Id>2</Id>
<FlightNumber>1121</FlightNumber>
</Segment>
</Segments>
</Flight>
<Flight>
<Segments>
<Segment>
<Id>3</Id>
<FlightNumber>2121</FlightNumber>
</Segment>
<Segment>
<Id>4</Id>
<FlightNumber>2584</FlightNumber>
</Segment>
</Segments>
</Flight>
<Flight>
<Segments>
<Segment>
<Id>5</Id>
<FlightNumber>2121</FlightNumber>
</Segment>
<Segment>
<Id>6</Id>
<FlightNumber>2584</FlightNumber>
</Segment>
<Segment>
<Id>7</Id>
<FlightNumber>2023</FlightNumber>
</Segment>
</Segments>
</Flight>
</Response>
</FilterResult>
I would like to receive below output.
Expected Output:
<FilterResult>
<ResultCount>1</ResultCount>
<Response>
<Flight>
<Segments>
<Segment>
<Id>3</Id>
<FlightNumber>2121</FlightNumber>
</Segment>
<Segment>
<Id>4</Id>
<FlightNumber>2584</FlightNumber>
</Segment>
</Segments>
</Flight>
</Response>
</FilterResult>
Please help me. How should i use the array to filter the response and get expected output. Thanks.
Flightnode.