我正在尝试获取与名称匹配的平面列表,并且在我的XML中,一个平面存在两次,在我的列表中,它在HTML中输出两次,我怎样才能让它只显示一次?我正在为飞机分配身份证号码,因此重复的号码将具有相同的身份证号码。
这是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>
答案 0 :(得分:2)
你可以使用&#34; Muenchian分组&#34;实现这一目标的技巧。在顶层定义一个键:
<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
模板应用于每个planeid的第一个平面元素的效果。