在xslt 1.0中,按多个相同子项的属性对元素进行分组



我有一个XML,它看起来像这样:

<?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>

我需要创建一个xslt,对共享其航段航班号的"行程"节点进行分组。

最终,我需要以下html输出:

<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>

但现在,我已经做到了:

<?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> 

但我的键一定有问题,因为我得到了一个空结果,只是第一个空表,但调试器没有显示任何编译错误或代码问题。

我一直在寻找不同的muenchian分组和复合键的方法,但我找不到解决方案。我甚至不知道为什么我的钥匙不起作用,所以你能给我的任何帮助都将不胜感激。

提前谢谢。

坦率地说,输入样本和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])]">

相关内容

  • 没有找到相关文章

最新更新