Reputation: 277
Given my input XML:
<?xml version="1.0" encoding="utf-8"?>
<FlightAvailability>
<FareDetails>
<Fare ID="2007" DepartureDate="2012-11-23T07:05:00">
<FareTypes>
<FareType FareType="Promo1">
<FareInfo Class="Y" FareBasis="Y" Fare="1500"/>
</FareType>
<FareType FareType="Promo2">
<FareInfo Class="Y" FareBasis="Y" Fare="1000"/>
</FareType>
</FareTypes>
</Fare>
<Fare ID="2008" DepartureDate="2012-11-23T08:00:00">
<FareTypes>
<FareType FareType="Promo1">
<FareInfo Class="Y" FareBasis="Y" Fare="2500"/>
</FareType>
<FareType FareType="Promo2">
<FareInfo Class="Y" FareBasis="Y" Fare="2000"/>
</FareType>
</FareTypes>
</Fare>
</FareDetails>
<SegmentDetails>
<Segment ID="2007" Origin="DEL" Destination="BOM"
DepartureDate="2012-11-23T07:05:00" Airline="YY" ArrivalDate="2012-11-23T08:55:00"
Stops="0" AircraftType="320"
FlightNum="100"/>
<Segment ID="2008" Origin="DEL" Destination="BOM"
DepartureDate="2012-11-23T08:00:00" Airline="YY" ArrivalDate="2012-11-23T09:55:00"
Stops="0" AircraftType="320" FlightNum="200"/>
</SegmentDetails>
</FlightAvailability>
I wanted to create an output XML wherein I pick up FareTypes of FareType "Promo1" and find the corresponding Segment by matching the ID (eg. 2007 in Fare and Segment tags). While I iterate for-each in the FareDetails/Fare and pick up the ID, how do I pass the ID and pick up the particular Segment information. I am new to XSL and know this should probably done making a call to template with-param, where the param is the ID, but not able to set it up. I tried to pass a variable, but it doesn't pick anything from the Segments.
This is an XSL I tried:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output version="1.0" method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:element name="AvailRS">
<xsl:element name="Availability">
<xsl:for-each select="//FareDetails/Fare">
<xsl:element name="item">
<xsl:element name="Fares">
<xsl:variable name = "lfid" select="@ID"/>
<xsl:element name = "ID"><xsl:value-of select="$lfid"/></xsl:element>
<xsl:for-each select="FareTypes/FareType[@FareType='Promo1']">
<xsl:element name="item">
<xsl:element name="BaseAmount">
<xsl:value-of select="ceiling(FareInfo/@Fare)"/>
</xsl:element>
<xsl:element name="FareBasisCode">
<xsl:value-of select="FareInfo/@FareBasis"/>
</xsl:element>
<xsl:element name="FareClass">
<xsl:value-of select="FareInfo/@Class"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
<xsl:element name="Flights">
<xsl:variable name="seg" select="//SegmentDetails/Segment[@ID='$lfid']"/>
<xsl:element name="item">
<xsl:element name="FlightNumber"><xsl:value-of select="$seg/@FlightNum"/></xsl:element>
<xsl:element name="DepTime"><xsl:value-of select="$seg/@DepartureDate"/></xsl:element>
<xsl:element name="ArrTime"><xsl:value-of select="$seg/@ArrivalDate"/></xsl:element>
<xsl:element name="Origin"><xsl:value-of select="$seg/@Origin"/></xsl:element>
<xsl:element name="Destination"><xsl:value-of select="$seg/@Destination"/></xsl:element>
<xsl:element name="Stops"><xsl:value-of select="$seg/@Stops"/></xsl:element>
<xsl:element name="Aircraft"><xsl:value-of select="$seg/@AircraftType"/></xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
<Status>Success</Status>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This doesn't pick the data from Segment:
<AvailRS>
<Availability>
<item>
<Fares>
<ID>2007</ID>
<item>
<BaseAmount>1500</BaseAmount>
<FareBasisCode>Y</FareBasisCode>
<FareClass>Y</FareClass>
</item>
</Fares>
<Flights>
<item>
<FlightNumber></FlightNumber>
<DepTime></DepTime>
<ArrTime></ArrTime>
<Origin></Origin>
<Destination></Destination>
<Stops></Stops>
<Aircraft></Aircraft>
</item>
</Flights>
</item>
<item>
<Fares>
<ID>2008</ID>
<item>
<BaseAmount>2500</BaseAmount>
<FareBasisCode>Y</FareBasisCode>
<FareClass>Y</FareClass>
</item>
</Fares>
<Flights>
<item>
<FlightNumber></FlightNumber>
<DepTime></DepTime>
<ArrTime></ArrTime>
<Origin></Origin>
<Destination></Destination>
<Stops></Stops>
<Aircraft></Aircraft>
</item>
</Flights>
</item>
</Availability>
<Status>Success</Status>
</AvailRS>
What I would like to get in the above example would be the Segment information corresponding to the Fare connected by the ID attribute (2007, 2008), but the $id variable isn't going to work. How do I select the corresponding ID?
Upvotes: 2
Views: 1058
Reputation: 28004
You need to remove the quotes around the variable reference. Change
//SegmentDetails/Segment[@ID='$lfid']
to
//SegmentDetails/Segment[@ID = $lfid]
Otherwise, you are asking XSLT to compare the value of the ID attribute with the literal string $lfid
.
Upvotes: 2