无法在for循环之外访问xsl变量,任何其他解决方案



我正在尝试执行一个逻辑,以根据行程确定旅行类型。条件是:如果有多个行程AND1.如果出发机场(/TopSection/troiraryInfo[1]/seageDetails/boardPointDetails/trueLocationId)没有重复,出发国家(/TopSection/countryListInfo/countryCode[1]我不想得到我为循环内部分配的变量,我知道它的作用域只是那个特定的函数。但我想知道这个问题还有其他选择吗

我正在尝试/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

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

根据您的编辑进行编辑:

以下是如何测试第一个行程的出发机场在后续行程的任何机场(出发地或目的地)都不会重复的方法:

<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列表中的第一个国家/地区代码是否稍后在同一列表中重复。

相关内容

  • 没有找到相关文章

最新更新