我是 XSLT 的初学者。
下面是我收到的源 XML。Request
tag 包含FlightId
用于过滤Result
标记的哪个。
源 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>
我使用上面的 XSLT 收到了以下输出。
输出:
<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>
我想收到以下输出。
预期输出:
<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>
请帮我。我应该如何使用数组来过滤响应并获得预期的输出。谢谢。