xml - Select distinct elements in Xpath from an id attribute, using Xpath 1.0? -
i trying list of planes match name, , in xml plane exists twice, in list beingness output twice in html, how can display once? assigning id numbers planes, ones repeat have same id number.
here 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>
here 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>
you can utilize "muenchian grouping" trick accomplish this. define key @ top level:
<xsl:key name="planebyid" match="plane" use="@planeid"/>
then instead of
<xsl:for-each select="flights/flight"> <xsl:apply-templates select="plane" /> </xsl:for-each>
just say
<xsl:apply-templates select="flights/flight/plane[generate-id() = generate-id(key('planebyid', @planeid)[1])]"/>
this has effect of applying plane
template first plane element each planeid.
xml xslt xpath
No comments:
Post a Comment