2013-02-16 63 views
0

我想獲得一個匹配名稱的平面列表,並且在我的XML平面中存在兩次,在我的列表中它正在HTML中輸出兩次,我怎樣才能讓它只顯示一次?我將ID號碼分配給飛機,所以重複的號碼將具有相同的ID號碼。使用Xpath 1.0從id屬性中選擇Xpath中的不同元素?

這裏是XML:

<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="1"> 
     <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> 

這裏是XPath:

<xsl:element name="ul"> 
      <xsl:attribute name="style"> 
       <xsl:text>width:100px; margin:0 auto; padding: 0;</xsl:text> 
      </xsl:attribute> 
       <xsl:for-each select="flights/flight"> 
        <xsl:apply-templates select="plane" /> 
       </xsl:for-each> 
     </xsl:element> 


<xsl:template match="plane"> 
    <xsl:element name="li"> 
     <xsl:attribute name="style"> 
      <xsl:text>list-style-type:none; width:100px; margin:0 auto; margin-top: 20px;</xsl:text> 
     </xsl:attribute> 
     <a><xsl:attribute name="href">aircraft.php?a=<xsl:value-of select="name" /></xsl:attribute> 
      <xsl:value-of select="name" /> 
     </a> 
    </xsl:element> 
</xsl:template> 

回答

2

您可以使用 「Muenchian分組的」 絕招實現這一目標。在頂層定義鍵:

<xsl:key name="planeById" match="plane" use="@planeid"/> 

然後,而不是

<xsl:for-each select="flights/flight"> 
    <xsl:apply-templates select="plane" /> 
</xsl:for-each> 

只是說

<xsl:apply-templates select="flights/flight/plane[generate-id() = 
    generate-id(key('planeById', @planeid)[1])]"/> 

這有應用plane模板僅第一平面元素與效果每個飛機。

+0

你是一個完美的天才!你早已爲我節省了很多時間,但我無法做到這一點。謝謝!! – deucalion0 2013-02-16 14:59:32