I am beginner in XSLT 1.0. I am facing an issue when i tried to find unique node with Highest value.
Source XML:
<Response>
<Flight>
<From>A</From>
<To>B</To>
<Passengers>
<Passenger>
<Fare>200</Fare>
<Fee>15</Fee>
<Type>ADT</Type>
</Passenger>
<Passenger>
<Fare>200</Fare>
<Fee>15</Fee>
<Type>ADT</Type>
</Passenger>
<Passenger>
<Fare>150</Fare>
<Fee>15</Fee>
<Type>CHD</Type>
</Passenger>
</Passengers>
</Flight>
<Flight>
<From>B</From>
<To>A</To>
<Passengers>
<Passenger>
<Fare>250</Fare>
<Fee>25</Fee>
<Type>ADT</Type>
</Passenger>
<Passenger>
<Fare>250</Fare>
<Fee>25</Fee>
<Type>ADT</Type>
</Passenger>
<Passenger>
<Fare>100</Fare>
<Fee>25</Fee>
<Type>CHD</Type>
</Passenger>
</Passengers>
</Flight>
</Response>
As shown in above XML, There is a round trip result for A-B origin destination and i am going to purchase tickets for both the flights. i need to calculate total fare, fee (whichever is highest from both the Flights) and its total price.
Expected Output:
<Result>
<PassengerGroup TotalFare="1150" TotalFee="75" TotalPrice="1225"/>
</Result>
Here,
TotalFare
is sum of all theFare
tag.TotalFee
is greater value of each passenger type multiply by no of passenger of that type.- e.g. For ADT greater Fee value is 25 and number of ADT is 2 so total Fee for ADT is 50. For CHD greater Fee value is 25 and number of CHD is 1 so total Fee for CHD is 25. That makes final total fee = 75.
TotalPrice
is sum ofToatalFare
andTotalFee
attribute.
I've tried with below XSLT.
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt">
<xsl:output method="xml" indent="yes" />
<xsl:template match="Response">
<xsl:element name="Result">
<xsl:element name="PassengerGroup">
<xsl:attribute name="TotalFare">
<xsl:value-of select="sum(//Flight/Passengers/Passenger/Fare)"/>
</xsl:attribute>
<xsl:attribute name="TotalFee">
<xsl:value-of select="sum(//Flight/Passengers/Passenger[not(Type=preceding::Passenger/Type)]/Fee)"/>
</xsl:attribute>
<xsl:attribute name="TotalFee">
<xsl:value-of select="sum(//Flight/Passengers/Passenger/Fare) + sum(//Flight/Passengers/Passenger/Fee)"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
and got the below output.
Output:
<Result>
<PassengerGroup TotalFare="1150" TotalFee="30" TotalPrice="1270"/>
</Result>
TotalFee
is not calculated correctly. Please Help. Thanks.