I need to create a table in html from an XSLT file, but selecting only a specific group of nodes. For example in the following XML I wish to select all of the from and to child nodes where routename contains fco-dxb:
<?xml version="1.0" encoding="UTF-8"?>
<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 A380-861</name>
<registereddate>07-06-10</registereddate>
</plane>
<registration>3A6-EDJ</registration>
<altitude height="feet">41000</altitude>
<speed ratio="mph">564</speed>
<distance unit="miles">erf</distance>
<route>
<routename>FCO-DXB</routename>
<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>United Arab Emirates</country>
<city>Dubai</city>
<latitude>25.2528</latitude>
<longitude>55.3644</longitude>
</to>
</route>
<course bearing="degrees">154</course>
<journey>
<distance type="miles">2,697</distance>
<time>PT5H30M</time>
</journey>
</flight>
<flight flightid="2">
<flightno>BA283</flightno>
<callsign>BAW283</callsign>
<airline>British Airways</airline>
<plane planeid="2">
<name>Boeing 747-436</name>
<registereddate>06-12-97</registereddate>
</plane>
<registration>3A6-EDJ</registration>
<altitude height="feet">41000</altitude>
<speed ratio="mph">564</speed>
<distance unit="miles">erf</distance>
<route>
<routename>LHR-LAX</routename>
<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 International</airport>
<country>United States of America</country>
<city>L.A</city>
<latitude>33.9471</latitude>
<longitude>-118.4082</longitude>
</to>
</route>
<course bearing="degrees">154</course>
<journey>
<distance type="miles">5,441 miles</distance>
<time>PT11H5M</time>
</journey>
</flight>
</flights>
and output this in a table, here is my attempt in the XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:element name="html">
<xsl:element name="head">
<xsl:element name="title">flights</xsl:element>
</xsl:element>
<xsl:element name="body">
<xsl:element name="table"><xsl:attribute name="border">1</xsl:attribute>
<xsl:element name="tr">
<xsl:element name="td">
<xsl:element name="b">Title</xsl:element>
</xsl:element>
<xsl:element name="td">
<xsl:element name="b">Artist</xsl:element>
</xsl:element>
<xsl:element name="td">
<xsl:element name="b">Year</xsl:element>
</xsl:element>
</xsl:element>
<xsl:apply-templates select="flights/flight/route[contains(text(), 'FCO-DXB')]" />
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="from">
<xsl:element name="tr">
<xsl:element name="td"><xsl:value-of select="iatacode"/></xsl:element>
<xsl:element name="td"><xsl:value-of select="airport"/></xsl:element>
<xsl:element name="td"><xsl:value-of select="country"/></xsl:element>
<xsl:element name="td"><xsl:value-of select="city"/></xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="to">
<xsl:element name="tr">
<xsl:element name="td"><xsl:value-of select="iatacode"/></xsl:element>
<xsl:element name="td"><xsl:value-of select="airport"/></xsl:element>
<xsl:element name="td"><xsl:value-of select="country"/></xsl:element>
<xsl:element name="td"><xsl:value-of select="city"/></xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
My goal is to eventually figure out how to pass parameters to the XSLT file so instead of hardcoding the contains text, I can pass it a parameter, but so far I would just like to know how to answer this question.
You did not say if there can be more than one routename with the same identifier. And if there are, they should be displayed in the same table or in a different table (one per match)?
I assume here that if there are multiple routenames with the same identifier, those are printed in the same table (correct me if this assumption is not right and I will change the code accordingly).
This is one way of doing what you are trying to achieve: