XSL for-each仅获取第一个节点值

时间:2013-02-16 12:42:23

标签: xml xslt xpath

以下是我的XSLT文件中的foreach:

<xsl:for-each select="/flights/flight/route[../plane/name/. ='Airbus 330']/routename"> 
        <tr>
            <td>
                <xsl:attribute name="colspan" >2</xsl:attribute><xsl:attribute name="style" >text-align:center</xsl:attribute><a><xsl:attribute name="href">map.php?a=<xsl:value-of select="/flights/flight/route[../plane/name/. =$a]/from/latitude" />&amp;b=<xsl:value-of select="/flights/flight/route[../plane/name/. =$a]/from/longitude" />&amp;c=<xsl:value-of select="/flights/flight/route[../plane/name/. =$a]/to/latitude" />&amp;d=<xsl:value-of select="/flights/flight/route[../plane/name/. =$a]/to/longitude" />&amp;e=<xsl:value-of select="/flights/flight/route[../plane/name/. =$a]/routename" /></xsl:attribute>
                <xsl:value-of select="/flights/flight/route[../plane/name/. ='Airbus 330']/routename"/></a>
            </td>
        </tr>
    </xsl:for-each> 

有两条路由有空中客车330平面,当为每个路由运行时,它会根据需要创建两个表行和链接,但使用第一个路由名两次,而不是每个路由名。为什么会这样?

这是XML文件:

<?xml version="1.0" encoding="UTF-8"?>

<flights
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="flights.xsd">

<flight flightid="1">
    <flightno>EK98</flightno>
    <callsign>UAE98</callsign>
    <airline>Emirates Airline</airline>

    <plane planeid="1">
        <name>Airbus 330</name>
        <speed>567</speed>
        <registereddate>07-06-10</registereddate>
    </plane>

    <registration>3A6-EDJ</registration>
    <altitude height="feet">41000 feet</altitude>
    <speed ratio="mph">564 mph</speed>

    <route>
    <routename>Fiumicino-Dubai</routename>
    <course bearing="degrees">154 degrees</course>
    <distance type="miles">2697 miles</distance> 
    <duration>PT5H30M</duration>

        <from>
            <iatacode>FCO</iatacode>
            <airport>Fiumicino</airport>
            <country>Italy</country>
            <city>Rome</city>
            <latitude>41.8044</latitude>
            <longitude>12.2508</longitude>
        </from>

        <to>
            <iatacode>DXB</iatacode>
            <airport>Dubai Intl</airport>
            <country>UAE</country>
            <city>Dubai</city>
            <latitude>25.2528</latitude>
            <longitude>55.3644</longitude>
        </to>
    </route>

</flight>


<flight flightid="2">
    <flightno>BA283</flightno>
    <callsign>BAW283</callsign>
    <airline>British Airways</airline>

    <plane planeid="2">
        <name>Airbus 330</name>
            <speed>567</speed>
        <registereddate>06-12-97</registereddate>
    </plane>


    <registration>3A6-EDJ</registration>
    <altitude height="feet">41000 feet</altitude>
    <speed ratio="mph">564 mph</speed>

    <route>
        <routename>London-L.A</routename>
        <course bearing="degrees">154 degrees</course>
        <distance type="miles">5441 miles</distance> 
        <time>PT11H5M</time>
        <from>

            <iatacode>LHR</iatacode>
            <airport>Heathrow</airport>
            <country>England</country>
            <city>London</city>
            <latitude>51.4775</latitude>
            <longitude>0.4614</longitude>
        </from>

        <to>
            <iatacode>LAX</iatacode>
            <airport>Los Angeles Intl</airport>
            <country>USA</country>
            <city>L.A</city>
            <latitude>33.9471</latitude>
            <longitude>-118.4082</longitude>
        </to>
    </route>

</flight>

</flights>

1 个答案:

答案 0 :(得分:2)

只需将<xsl:value-of select="/flights/flight/route[../plane/name/. ='Airbus 330']/routename"/></a>替换为<xsl:value-of select="."/>即可。在for-eachroutename是上下文节点,您想要输出它(而不是在文档中开始新的导航)。