sarma
sarma

Reputation: 63

not able to access xsl variable outside for-loop , any alternate solution?

i am trying to execute a logic to determine the trip type based on itinerary. condition is: if there is more than one itinerary AND 1.if departure airport(/TopSection/itineraryInfo[1]/segmentDetails/boardPointDetails/trueLocationId) is not repeated and departure country(/TopSection/countryListInfo/countryCode[1]) is not repeated then its oneway, all others are two-way (it is simplified to the context) I am not to get the variable which i assigned inside for-loop, I know its scope is only to that specific function. But I am trying to know is there any alternative to this problem

I am trying whether /TopSection/itineraryInfo[1]/segmentDetails/boardPointDetails/trueLocationId is repeating in any of below places.

/TopSection/itineraryInfo[1]/segmentDetails/offpointDetails/trueLocationId
/TopSection/itineraryInfo[2]/segmentDetails/boardPointDetails/trueLocationId
/TopSection/itineraryInfo[2]/segmentDetails/offpointDetails/trueLocationId
/TopSection/itineraryInfo[3]/segmentDetails/boardPointDetails/trueLocationId
/TopSection/itineraryInfo[3]/segmentDetails/offpointDetails/trueLocationId
/TopSection/itineraryInfo[4]/segmentDetails/boardPointDetails/trueLocationId
/TopSection/itineraryInfo[4]/segmentDetails/offpointDetails/trueLocationId

INPUT XML:

<TopSection>
<countryListInfo>
    <countryCode>FR</countryCode>
    <countryCode>UK</countryCode>
    <countryCode>US</countryCode>       
    <countryCode>FR</countryCode>   
</countryListInfo>
<itineraryInfo>
    <segmentDetails>            
        <boardPointDetails>
            <trueLocationId>JFK</trueLocationId>
        </boardPointDetails>
        <offpointDetails>
            <trueLocationId>FRA</trueLocationId>
        </offpointDetails>
    </segmentDetails>
</itineraryInfo>
<itineraryInfo>
    <segmentDetails>            
        <boardPointDetails>
            <trueLocationId>FRA</trueLocationId>
        </boardPointDetails>
        <offpointDetails>
            <trueLocationId>LHR</trueLocationId>
        </offpointDetails>
    </segmentDetails>
</itineraryInfo>
<itineraryInfo>
    <segmentDetails>            
        <boardPointDetails>
            <trueLocationId>LHR</trueLocationId>
        </boardPointDetails>
        <offpointDetails>
            <trueLocationId>JFK</trueLocationId>
        </offpointDetails>
    </segmentDetails>
</itineraryInfo>

XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="http://exslt.org/dates-and-times"
    extension-element-prefixes="date" version="1.0">

    <xsl:output method="html" omit-xml-declaration="yes" />

    <xsl:template match="/">
        <h2>Country list</h2>
        <!-- ${workspace_loc:/Test_XML/XSLtesting/tripType.xml} -->

        Number of Iteneraries:
        <xsl:value-of select="count(/TopSection/itineraryInfo)" />
        <xsl:variable name="numberOfIteneraries">
            <xsl:value-of select="count(/TopSection/itineraryInfo)" />
        </xsl:variable>
        <xsl:variable name="first_country_countryList">
            <xsl:value-of select="/TopSection/countryListInfo/countryCode[1]" />
        </xsl:variable>
        <xsl:variable name="first_country_repeated" select="'NO'" />
        <xsl:variable name="Departure_Airport_repeated" select="'NO'" />

        first_country_countryList :
        <xsl:value-of select="$first_country_countryList" />
        <xsl:for-each select="/TopSection/countryListInfo/countryCode">
            followingNode:
            <xsl:value-of select="following-sibling::countryCode[.]" />
            <xsl:variable name="followingNode">
                <xsl:value-of select="following-sibling::countryCode[.]" />
            </xsl:variable>

            <xsl:if test="$followingNode=$first_country_countryList">
                <xsl:text> first country repeated</xsl:text>
                <xsl:variable name="first_country_repeated" select="'YES'" />
            </xsl:if>
        </xsl:for-each>

        first_country_repeated :
        <xsl:value-of select="$first_country_repeated" />

        <xsl:variable name="Departure_Airport">
            <xsl:value-of
                select="/TopSection/itineraryInfo[1]/segmentDetails/boardPointDetails/trueLocationId" />
        </xsl:variable>
        <xsl:for-each select="/TopSection/itineraryInfo">
            <xsl:variable name="FOR_Departure_Airport">
                <xsl:value-of
                    select="following-sibling::itineraryInfo/segmentDetails/boardPointDetails/trueLocationId[.]" />
            </xsl:variable>
            <xsl:variable name="FOR_Arrival_Airport">
                <xsl:value-of
                    select="following-sibling::itineraryInfo/segmentDetails/offpointDetails/trueLocationId[.]" />
            </xsl:variable>
            <xsl:if
                test="$Departure_Airport=$FOR_Departure_Airport or $Departure_Airport=$FOR_Arrival_Airport">
                <xsl:text> first Departure_Airport repeated</xsl:text>
                <xsl:variable name="Departure_Airport_repeated" select="'YES'" />
            </xsl:if>
        </xsl:for-each>
        Departure_Airport_repeated :
        <xsl:value-of select="$Departure_Airport_repeated" />

        <xsl:variable name="tripTypeDetermined">
            <xsl:choose>
                <xsl:when test="$numberOfIteneraries=1">
                    <xsl:value-of select="'OW'" />
                </xsl:when>
                <xsl:when
                    test="$numberOfIteneraries > 1 and not($first_country_repeated='YES') and not($Departure_Airport_repeated='YES')">
                    <xsl:value-of select="'OW'" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'RT'" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        tripTypeDetermined :
        <xsl:value-of select="$tripTypeDetermined" />


    </xsl:template>

</xsl:stylesheet>

Output I am getting right now:

<h2>Country list</h2>


        Number of Iteneraries:
        0

        first_country_countryList :


        first_country_repeated :
        NO
        Departure_Airport_repeated :
        NO

        tripTypeDetermined :
        RT

Expecting OUTPUT:

Country list

    Number of Iteneraries:
    3

    first_country_countryList :FR


    first_country_repeated :
    YES
    Departure_Airport_repeated :
    YES

    tripTypeDetermined :
    RT

Upvotes: 0

Views: 199

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117003

edited in response to your edit:

Here is how you can test that the first itinerary's departure airport is not repeated in any of the subsequent itineraries' airports - either departure or destination:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="airport" match="trueLocationId" use="." />

<xsl:template match="/">
    <test>
        <xsl:value-of select="count(key('airport', TopSection/itineraryInfo[1]/segmentDetails/boardPointDetails/trueLocationId))=1" />
    </test>
</xsl:template>

</xsl:stylesheet>

Similarly,if you define another key as:

<xsl:key name="country" match="countryCode" use="." />

you can then use:

count(key('country', TopSection/countryListInfo/countryCode[1]))=1

to test that the first country code in the countryListInfo list is not repeated later in the same list.

Upvotes: 1

Related Questions