2013-02-16 106 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