2013-02-16 248 views
0

这里是我的XSLT文件中的foreach:XSL的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飞机,运行这个每个时,它会创建两个表格行和链接,但是使用第一个routename而不是每个routename。这是为什么发生?

下面是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> 

回答

2

只需使用<xsl:value-of select="."/>取代<xsl:value-of select="/flights/flight/route[../plane/name/. ='Airbus 330']/routename"/></a>。在for-eachroutename是上下文节点,并且您要输出该文件(而不是通过文档启动新的导航)。

+0

感谢那些工作,链接现在是两个不同的路线名称,但传递的参数仍然是第一套,你能告诉我如何遍历到下一组的参数,我试过,但没有奏效:map.php一个=的? – deucalion0 2013-02-16 13:14:13

+1

当你处理'routename'作为上下文节点,则需要'的',然后到达父路由,然后到'from/latitude'子孙,或者你可以访问兄弟''。作为第三种选择,你可以改变'for-each'来处理'route'元素,然后你可以使用select <= routename“/>'和'。 – 2013-02-16 14:18:11

+0

非常感谢您的帮助,我也对此更加了解。谢谢!! – deucalion0 2013-02-16 14:30:41