简体   繁体   English

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

[英]Grouping elements by attribute of multiple identical childs in xslt 1.0

I've got an XML that looks like this: 我有一个看起来像这样的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>

And I need to create a xslt that groups the "itinerary" nodes that share the flight numbers of their flight segments. 而且我需要创建一个xslt来对共享其航班段航班号的“初始”节点进行分组。

Eventually, i need the following html output: 最终,我需要以下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>

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. 我一直在寻找muenchian分组和复合键的不同方法,但是我找不到解决方案。 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. 提前致谢。

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. 坦白说,输入样本和XSLT样本的格式都不正确,因此很难想象您可以得到任何输出。

But given <xsl:key name="groups" match="Itinerary" use="./Details/FlightSegment/@FlightNumber"/> the usual approach is to use 但是给定<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])]">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM