Saturday, 15 January 2011

How to correctly use XML attributes when using XSLT -



How to correctly use XML attributes when using XSLT -

i made xml file illustration of here:

<flight flightid="1"> <flightno>ek98</flightno> <callsign>uae98</callsign> <airline>emirates airline</airline> <altitude height="feet">41000 feet</altitude> <speed ratio="mph">564 mph</speed> <plane planeid="1"> <name>airbus 330</name> <speed>567 mph</speed> <wingspan>199 ft 11 in</wingspan> <length>301 ft 7 in</length> <registereddate>07-06-10</registereddate> </plane> <route> <routename>fiumicino-dubai</routename> <course bearing="degrees">154 degrees</course> <distance unit="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> <yahoowoeid>715520</yahoowoeid> </from> <to> <iatacode>dxb</iatacode> <airport>dubai intl</airport> <country>uae</country> <city>dubai</city> <latitude>25.2528</latitude> <longitude>55.3644</longitude> <yahoowoeid>1940345</yahoowoeid> </to> </route> </flight>

in distance element can see unit miles:

<distance unit="miles">2697 miles</distance>

in text have written miles, felt point in attributes can utilize , output value of attribute alongside value in distance? here sample of xsl distance used , effort @ adding units:

<tr> <td><xsl:attribute name="class">lside</xsl:attribute>distance</td> <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. ="fiumicino-dubai"]/distance"/><xsl:value-of select="@unit" /></td> </tr>

while inquire question, initial intention right way utilize attributes, why attributes exist, create global type of measurement same?

an attribute short form of element-value pair in xml document. <distance unit="miles">2697 miles</distance> same writing following:

<distance> <unit> <value>miles</value> </unit> <value>2697</value> </distance>

yes, using element attribute correctly in above xml. may take form xml document anyway wish--and hence pull info of such accordingly.

transfering reply in comments below answer:

to access value of element specific attribute using xpath, utilize below distinguish:

"/flights/flight/route[routename/. ="fiumicino-dubai"]/distance/@unit=@miles

xml xslt xpath

No comments:

Post a Comment