Question

I've got an XML that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Itineraries>
  <Itinerary>
    <Details>
      <FlightSegment DepartureDateTime="2014-05-02T14:24:00" ArrivalDateTime="2014-05-02T15:42:00" FlightNumber="5214">
        <DepartureAirport LocationCode="JFK" />
        <ArrivalAirport LocationCode="IAD" />
        <OperatingAirline Code="UA">
          </FlightSegment>
      <FlightSegment DepartureDateTime="2014-05-02T16:24:00" ArrivalDateTime="2014-05-02T17:42:00" FlightNumber="54">
        <DepartureAirport LocationCode="IAD" />
        <ArrivalAirport LocationCode="MEX" />
        <OperatingAirline Code="UA">
          </FlightSegment>
    </Details>
    <Price>
      <Base BasePr="250">
        <Total TotalPr="320">
          <Price>
       </Itinerary>
  <Itinerary>
    <Details>
      <FlightSegment DepartureDateTime="2014-06-02T14:24:00" ArrivalDateTime="2014-06-02T15:42:00" FlightNumber="725">
        <DepartureAirport LocationCode="JFK" />
        <ArrivalAirport LocationCode="IAD" />
        <OperatingAirline Code="UA">
          </FlightSegment>
      <FlightSegment DepartureDateTime="2014-06-02T16:24:00" ArrivalDateTime="2014-06-02T17:42:00" FlightNumber="245">
        <DepartureAirport LocationCode="IAD" />
        <ArrivalAirport LocationCode="MEX" />
        <OperatingAirline Code="UA">
          </FlightSegment>
    </Details>
    <Price>
      <Base BasePr="240">
        <Total TotalPr="310">
          <Price>
            <Itinerary>
              <Details>
                <FlightSegment DepartureDateTime="2014-06-02T14:24:00" ArrivalDateTime="2014-06-02T15:42:00" FlightNumber="5124">
                  <DepartureAirport LocationCode="JFK" />
                  <ArrivalAirport LocationCode="IAD" />
                  <OperatingAirline Code="UA">
          </FlightSegment>
                <FlightSegment DepartureDateTime="2014-06-02T16:24:00" ArrivalDateTime="2014-06-02T17:42:00" FlightNumber="54">
                  <DepartureAirport LocationCode="IAD" />
                  <ArrivalAirport LocationCode="MEX" />
                  <OperatingAirline Code="UA">
          </FlightSegment>
              </Details>
              <Price>
                <Base BasePr="230">
                  <Total TotalPr="300">
                    <Price>
       </Itinerary>
            <Itinerary>
              <Details>
                <FlightSegment DepartureDateTime="2014-05-02T14:24:00" ArrivalDateTime="2014-05-02T15:42:00" FlightNumber="725">
                  <DepartureAirport LocationCode="JFK" />
                  <ArrivalAirport LocationCode="IAD" />
                  <OperatingAirline Code="UA">
          </FlightSegment>
                <FlightSegment DepartureDateTime="2014-05-02T16:24:00" ArrivalDateTime="2014-05-02T17:42:00" FlightNumber="245">
                  <DepartureAirport LocationCode="IAD" />
                  <ArrivalAirport LocationCode="MEX" />
                  <OperatingAirline Code="UA">
          </FlightSegment>
              </Details>
              <Price>
                <Base BasePr="220">
                  <Total TotalPr="290">
                    <Price>
       </Itinerary>
          </Itineraries>

And I need to create a xslt that groups the "itinerary" nodes that share the flight numbers of their flight segments.

Eventually, i need the following html output:

<table>
  <table>
    <tr>
      <td>
        Flight: 5124
      </td>
      <td>
        JFK - IAD
      </td>
    </tr>
    <tr>
      <td>
        Airline: UA
      </td>
    </tr>
    <tr>
      <td>
        Departure: 14:24
      </td>
      <td>
        Arrival: 15:42
      </td>
    </tr>
    <tr>
      <td>
        Flight: 54
      </td>
      <td>
        IAD - MEX
      </td>
    </tr>
    <tr>
      <td>
        Airline: UA
      </td>
    </tr>
    <tr>
      <td>
        Departure: 16:24
      </td>
      <td>
        Arrival: 17:42
      </td>
    </tr>
  </table>
  <table>
    <tr>
      <td>
        <table>
          <tr>
            <td>2014-05-02</td>
          </tr>
          <tr>
            <td>Base</td>
            <td>250</td>
          </tr>
          <tr>
            <td>Total</td>
            <td>320</td>
          </tr>
        </table>
        <table>
          <tr>
            <td>2014-06-02</td>
          </tr>
          <tr>
            <td>Base</td>
            <td>230</td>
          </tr>
          <tr>
            <td>Total</td>
            <td>300</td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
  <table>
    <tr>
      <td>
        Flight: 725
      </td>
      <td>
        JFK - IAD
      </td>
    </tr>
    <tr>
      <td>
        Airline: UA
      </td>
    </tr>
    <tr>
      <td>
        Departure: 14:24
      </td>
      <td>
        Arrival: 15:42
      </td>
    </tr>
    <tr>
      <td>
        Flight: 245
      </td>
      <td>
        IAD - MEX
      </td>
    </tr>
    <tr>
      <td>
        Airline: UA
      </td>
    </tr>
    <tr>
      <td>
        Departure: 16:24
      </td>
      <td>
        Arrival: 17:42
      </td>
    </tr>
  </table>
  <table>
    <tr>
      <td>
        <table>
          <tr>
            <td>2014-05-02</td>
          </tr>
          <tr>
            <td>Base</td>
            <td>220</td>
          </tr>
          <tr>
            <td>Total</td>
            <td>290</td>
          </tr>
        </table>
        <table>
          <tr>
            <td>2014-06-02</td>
          </tr>
          <tr>
            <td>Base</td>
            <td>240</td>
          </tr>
          <tr>
            <td>Total</td>
            <td>310</td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</table>

But for now, i've got as far as:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes"/>
  <xsl:key name="groups" match="Itinerary" use="./Details/FlightSegment/@FlightNumber"/>
  <xsl:template match="/">

    <table>
      <tr>
        <td>
          <xsl:for-each select="/Itineraries/Itinerary[generate-id() = generate-id(key('groups', Itinerary)[1])]">
            <table>
              <tr>
                <td>
                  <xsl:value-of select="@FlightNumber"/>
                  <td>
 </tr>
            </table>
          </xsl:for-each>
        </td>
      </tr>
    </table>
  </xsl:template>

</xsl:stylesheet> 

But something must be wrong with my key, because i get a null result, just the first empty table, but the debugger does not show any compilation error or problem with my code.

I've been looking for differents methods of muenchian grouping and compound keys, but i can't find a solution. I don't even know why my key does not work, so any help you could give me would be greatly apreciated.

Thanks in advance.

Was it helpful?

Solution

Frankly both the input sample as well as the XSLT sample are not well-formed so it is hard to imagine that you could get any output at all.

But given <xsl:key name="groups" match="Itinerary" use="./Details/FlightSegment/@FlightNumber"/> the usual approach is to use

<xsl:for-each select="/Itineraries/Itinerary[generate-id() = generate-id(key('groups', Details/FlightSegment/@FlightNumber)[1])]">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top