0

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.

4

1 回答 1

0

坦率地说,输入样本和 XSLT 样本的格式都不是很好,因此很难想象您可以得到任何输出。

但鉴于<xsl:key name="groups" match="Itinerary" use="./Details/FlightSegment/@FlightNumber"/>通常的方法是使用

<xsl:for-each select="/Itineraries/Itinerary[generate-id() = generate-id(key('groups', Details/FlightSegment/@FlightNumber)[1])]">
于 2014-04-25T17:47:19.833 回答