无法访问for循环外的xsl变量,任何替代解决方案?

时间:2014-10-23 21:54:35

标签: xslt xslt-1.0

我正在尝试执行逻辑以根据行程确定行程类型。 条件是:如果有多个行程AND 1.如果出发机场(/ TopSection / itineraryInfo [1] / segmentDetails / boardPointDetails / trueLocationId)没有重复,出发国家(/ TopSection / countryListInfo / countryCode [1])不再重复,那么它的所有其他都是双向的(它简化为上下文) 我不是要获取我在for循环中分配的变量,我知道它的范围仅适用于该特定函数。但我想知道有没有替代这个问题

我正在尝试/TopSection/itineraryInfo[1]/segmentDetails/boardPointDetails/trueLocationId是否在以下任何地方重复。

/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>

输出我现在正在接受:

<h2>Country list</h2>


        Number of Iteneraries:
        0

        first_country_countryList :


        first_country_repeated :
        NO
        Departure_Airport_repeated :
        NO

        tripTypeDetermined :
        RT

期待输出:

国家/地区列表

    Number of Iteneraries:
    3

    first_country_countryList :FR


    first_country_repeated :
    YES
    Departure_Airport_repeated :
    YES

    tripTypeDetermined :
    RT

1 个答案:

答案 0 :(得分:1)

根据您的修改进行了编辑:

以下是您如何测试第一个行程的出发机场在任何后续行程中都不会重复的情况&#39;机场 - 出发地或目的地:

<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>

同样,如果您将另一个键定义为:

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

然后你可以使用:

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

以测试countryListInfo列表中的第一个国家/地区代码稍后不会在同一列表中重复。