2

嗨,首先我正在学习复杂的 XSLT 迭代,现在写我遇到了一个查询,其中我在标签信息下有相同的信息,唯一不同的标签是具有不同属性值的 BookingClassAvail,我想组合成一个保留信息标签作为单独的。

XML:

      <Availability>
        <Success/>
        <Information xmlns="http://www.opentravel.org/OTA/2003/05">
          <DepDateTime>2012-10-27</DepDateTime>
          <OrigLocation LocationCode="DEL" />
          <DestLocation LocationCode="BOM" />
          <OrigDestinationOptions>
            <OrigDestinationOption>
              <FlightSegment DepDateTime="2012-10-27A12:35:00" ArrDateTime="2012-10-27A14:05:00" StopQuantity="0" FlightNumber="152" JourneyDuration="90" Ticket="eTicket">
                <DepAirport LocationCode="DEL" Terminal="" />
                <ArrAirport LocationCode="BOM" Terminal="" />
                <OperatingAirline CompanyShortName="A1" />
                <Equipment AirEquipType="A320" />
                <MarketingAirline CompanyShortName="A1" />
                <BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180" />
              </FlightSegment>
            </OrigDestinationOption>
            <OrigDestinationOption>
              <FlightSegment DepDateTime="2012-10-27A12:35:00" ArrDateTime="2012-10-27A14:05:00" StopQuantity="0" FlightNumber="152" JourneyDuration="90" Ticket="eTicket">
                <DepAirport LocationCode="DEL" Terminal="" />
                <ArrAirport LocationCode="BOM" Terminal="" />
                <OperatingAirline CompanyShortName="A1" />
                <Equipment AirEquipType="A320" />
                <MarketingAirline CompanyShortName="A1" />
                <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80" />
              </FlightSegment>
            </OrigDestinationOption>


            <OrigDestinationOption>
              <FlightSegment DepDateTime="2012-10-28A10:35:00" ArrDateTime="2012-10-27A14:05:00" StopQuantity="0" FlightNumber="152" JourneyDuration="90" Ticket="eTicket">
                <DepAirport LocationCode="DEL" Terminal="" />
                <ArrAirport LocationCode="BOM" Terminal="" />
                <OperatingAirline CompanyShortName="A1" />
                <Equipment AirEquipType="A320" />
                <MarketingAirline CompanyShortName="A1" />
                <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80" />
              </FlightSegment>
            </OrigDestinationOption>
          </OrigDestinationOptions>
        </Information>
        <Information xmlns="http://www.opentravel.org/OTA/2003/05">
          <DepDateTime>2012-11-05</DepDateTime>
          <OrigLocation LocationCode="BOM" />
          <DestinationLocation LocationCode="DEL" />
          <OrigDestinationOptions>
            <OrigDestinationOption>
              <FlightSegment DepDateTime="2012-11-05T08:35:00" ArrDateTime="2012-11-05T09:40:00" StopQuantity="0" FlightNumber="993" JourneyDuration="65" Ticket="eTicket">
                <DepAirport LocationCode="BOM" Terminal="" />
                <ArrAirport LocationCode="DEL" Terminal="" />
                <OperatingAirline CompanyShortName="GR" />
                <Equipment AirEquipType="A320" />
                <MarketingAirline CompanyShortName="GR" />
                <BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180" />
              </FlightSegment>
            </OrigDestinationOption>
            <OrigDestinationOption>
              <FlightSegment DepDateTime="2012-11-05A08:35:00" ArrDateTime="2012-11-05A16:15:00" StopQuantity="0" FlightNumber="666" JourneyDuration="75" Ticket="eTicket">
                <DepAirport LocationCode="BOM" Terminal="" />
                <ArrAirport LocationCode="DEL" Terminal="" />
                <OperatingAirline CompanyShortName="MM" />
                <Equipment AirEquipType="A320" />
                <MarketingAirline CompanyShortName="MM" />
                <BookingClassAvail ResBookDesigCode="Y" ResBookDesigQuantity="180" />
              </FlightSegment>
            </OrigDestinationOption>
          </OrigDestinationOptions>
        </Information>
      </Availability>

想要如下输出:

    <Availability>
      <Success/>
      <Information xmlns="http://www.opentravel.org/OTA/2003/05">
        <DepDateTime>2012-10-27</DepDateTime>
        <OrigLocation LocationCode="DEL" />
        <DestLocation LocationCode="BOM" />
        <OrigDestinationOptions>
          <OrigDestinationOption>
            <FlightSegment DepDateTime="2012-10-27A12:35:00" ArrDateTime="2012-10-27A14:05:00" StopQuantity="0" FlightNumber="152" JourneyDuration="90" Ticket="eTicket">
              <DepAirport LocationCode="DEL" Terminal="" />
              <ArrAirport LocationCode="BOM" Terminal="" />
              <OperatingAirline CompanyShortName="A1" />
              <Equipment AirEquipType="A320" />
              <MarketingAirline CompanyShortName="A1" />
              <BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180" />
              <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80" />
            </FlightSegment>
          </OrigDestinationOption>

         <!-- since the DepDateTime has different date hence not combined in the above flightsegment -->

            <OrigDestinationOption>
              <FlightSegment DepDateTime="2012-10-28A10:35:00" ArrDateTime="2012-10-27A14:05:00" StopQuantity="0" FlightNumber="152" JourneyDuration="90" Ticket="eTicket">
                <DepAirport LocationCode="DEL" Terminal="" />
                <ArrAirport LocationCode="BOM" Terminal="" />
                <OperatingAirline CompanyShortName="A1" />
                <Equipment AirEquipType="A320" />
                <MarketingAirline CompanyShortName="A1" />
                <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80" />
              </FlightSegment>
            </OrigDestinationOption>
        </OrigDestinationOptions>
      </Information>
      <Information xmlns="http://www.opentravel.org/OTA/2003/05">
        <DepDateTime>2012-11-05</DepDateTime>
        <OrigLocation LocationCode="BOM" />
        <DestinationLocation LocationCode="DEL" />
        <OrigDestinationOptions>
          <OrigDestinationOption>
            <FlightSegment DepDateTime="2012-11-05T08:35:00" ArrDateTime="2012-11-05T09:40:00" StopQuantity="0" FlightNumber="993" JourneyDuration="65" Ticket="eTicket">
              <DepAirport LocationCode="BOM" Terminal="" />
              <ArrAirport LocationCode="DEL" Terminal="" />
              <OperatingAirline CompanyShortName="GR" />
              <Equipment AirEquipType="A320" />
              <MarketingAirline CompanyShortName="GR" />
              <BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180" />
              <BookingClassAvail ResBookDesigCode="Y" ResBookDesigQuantity="180" />
            </FlightSegment>
          </OrigDestinationOption>
        </OrigDestinationOptions>
      </Information>
    </Availability>

如果您在上面看到标签 BookingClassAvail 具有 ResBookDesigCode 和 ResBookDesigQuantity 的不同信息,因此需要逐行堆叠。

<BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180" />
<BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80" />

此外,如果其中的任何信息不同于它应该作为单独的航段出现。

还要注意,上面的一个标签为出港航班,其他信息为进港航班,需要单独保存,只需合并即可。

提前一百万谢谢。


肖恩,我使用了您建议的解决方案,但我遇到了一个错误,因为“无法从已加载的输入文档中删除空格。将输入文档提供为 XmlReader。”

XML:

        <Availability>
  <Success/>
  <Information xmlns="http://www.opentravel.org/OTA/2003/05">
    <DepDateTime>2012-10-27</DepDateTime>
    <OrigLocation LocationCode="DEL" />
    <DestinationLocation LocationCode="BOM" />
    <OrigDestinationOptions>
      <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-10-27T12:35:00" ArrivalDateTime="2012-10-27T14:05:00" StopQuantity="0" FlightNumber="152" JourneyDuration="90" Ticket="eTicket">
          <DepAirport LocationCode="DEL" Terminal="" />
          <ArrivalAirport LocationCode="BOM" Terminal="" />
          <OperatingAirline CompanyShortName="A1" />
          <Equipment AirEquipType="A123" />
          <MarketingAirline CompanyShortName="A1" />
          <BookingClassAvail ResBookDesigCode="O" ResBookDesigQuantity="40" />
        </FlightSegment>
      </OrigDestinationOption>
      <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-10-27T12:35:00" ArrivalDateTime="2012-10-27T14:05:00" StopQuantity="0" FlightNumber="152" JourneyDuration="90" Ticket="eTicket">
          <DepAirport LocationCode="DEL" Terminal="" />
          <ArrivalAirport LocationCode="BOM" Terminal="" />
          <OperatingAirline CompanyShortName="A1" />
          <Equipment AirEquipType="A123" />
          <MarketingAirline CompanyShortName="A1" />
          <BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180" />
        </FlightSegment>
      </OrigDestinationOption>
      <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-10-27T19:10:00" ArrivalDateTime="2012-10-27T20:10:00" StopQuantity="0" FlightNumber="158" JourneyDuration="60" Ticket="eTicket">
          <DepAirport LocationCode="DEL" Terminal="" />
          <ArrivalAirport LocationCode="BOM" Terminal="" />
          <OperatingAirline CompanyShortName="A1" />
          <Equipment AirEquipType="A123" />
          <MarketingAirline CompanyShortName="A1" />
          <BookingClassAvail ResBookDesigCode="D" ResBookDesigQuantity="50" />
        </FlightSegment>
      </OrigDestinationOption>
    </OrigDestinationOptions>
  </Information>
  <Information xmlns="http://www.opentravel.org/OTA/2003/05">
    <DepDateTime>2012-11-05</DepDateTime>
    <OrigLocation LocationCode="BOM" />
    <DestinationLocation LocationCode="DEL" />
    <OrigDestinationOptions>
      <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-11-05T15:00:00" ArrivalDateTime="2012-11-05T16:15:00" StopQuantity="0" FlightNumber="666" JourneyDuration="75" Ticket="eTicket">
          <DepAirport LocationCode="BOM" Terminal="" />
          <ArrivalAirport LocationCode="DEL" Terminal="" />
          <OperatingAirline CompanyShortName="M1" />
          <Equipment AirEquipType="A123" />
          <MarketingAirline CompanyShortName="M1" />
          <BookingClassAvail ResBookDesigCode="Y" ResBookDesigQuantity="180" />
        </FlightSegment>
      </OrigDestinationOption>
      <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-11-05T15:00:00" ArrivalDateTime="2012-11-05T16:15:00" StopQuantity="0" FlightNumber="666" JourneyDuration="75" Ticket="eTicket">
          <DepAirport LocationCode="BOM" Terminal="" />
          <ArrivalAirport LocationCode="DEL" Terminal="" />
          <OperatingAirline CompanyShortName="M1" />
          <Equipment AirEquipType="A123" />
          <MarketingAirline CompanyShortName="M1" />
          <BookingClassAvail ResBookDesigCode="Y" ResBookDesigQuantity="180" />
        </FlightSegment>
      </OrigDestinationOption>
    </OrigDestinationOptions>
  </Information>
</Availability>

XSLT 使用:

     <xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ot="http://www.opentravel.org/OTA/2003/05"
  exclude-result-prefixes="xsl ot">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*" />

  <xsl:key name="kSegment" match="ot:OrigDestinationOption"
                           use="concat( ot:FlightSegment[1]/@DepDateTime,
                                          '|', generate-id(..))" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ot:OrigDestinationOptions">
    <xsl:variable name="options" select="generate-id()" />
    <xsl:copy>
      <xsl:apply-templates select="
     ot:OrigDestinationOption[
       generate-id()=generate-id(key('kSegment',
        concat( ot:FlightSegment[1]/@DepDateTime,
                '|', $options))[1])]" mode="group" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ot:OrigDestinationOption" mode="group">
    <xsl:variable name="group" select="key('kSegment',
        concat( ot:FlightSegment[1]/@DepDateTime,'|', generate-id(..)))" />
    <xsl:copy>
      <xsl:apply-templates select="*[not(self::ot:FlightSegment)]"/>
      <FlightSegment xmlns="http://www.opentravel.org/OTA/2003/05">
        <xsl:apply-templates
          select="ot:FlightSegment[1]/@*  |
               ot:FlightSegment[1]/*[not(self::ot:BookingClassAvail)] |
               $group/ot:FlightSegment/ot:BookingClassAvail" />
      </FlightSegment>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

面临的错误:

“无法从已加载的输入文档中删除空白。将输入文档提供为 XmlReader。”

4

1 回答 1

0

这个 XSLT 1.0 样式表...

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ot="http://www.opentravel.org/OTA/2003/05"
  exclude-result-prefixes="xsl ot">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

    <xsl:key name="kSegment" match="ot:OrigDestinationOption"
                             use="concat( ot:FlightSegment[1]/@DepDateTime,
                                          '|', generate-id(..))" />

<xsl:template match="@*|node()">
 <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="ot:OrigDestinationOptions">
 <xsl:variable name="options" select="generate-id()" />
 <xsl:copy>
   <xsl:apply-templates select="
     ot:OrigDestinationOption[
       generate-id()=generate-id(key('kSegment',
        concat( ot:FlightSegment[1]/@DepDateTime,
                '|', $options))[1])]" mode="group" />
 </xsl:copy>
</xsl:template>

<xsl:template match="ot:OrigDestinationOption" mode="group">
  <xsl:variable name="group" select="key('kSegment',
        concat( ot:FlightSegment[1]/@DepDateTime,'|', generate-id(..)))" /> 
 <xsl:copy>
   <xsl:apply-templates select="*[not(self::ot:FlightSegment)]"/>
   <FlightSegment xmlns="http://www.opentravel.org/OTA/2003/05">
     <xsl:apply-templates
       select="ot:FlightSegment[1]/@*  |
               ot:FlightSegment[1]/*[not(self::ot:BookingClassAvail)] |
               $group/ot:FlightSegment/ot:BookingClassAvail" />
   </FlightSegment>  
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>

...当应用于此输入时(OP 提供的文档的缩减版)...

<Availability>
    <Information xmlns="http://www.opentravel.org/OTA/2003/05">
    <OrigDestinationOptions>
        <OrigDestinationOption>
            <FlightSegment DepDateTime="2012-10-27A12:35:00" Ticket="eTicket">
                <DepAirport LocationCode="DEL" Terminal=""/>
                <BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180"/>
            </FlightSegment>
        </OrigDestinationOption>
        <OrigDestinationOption>
            <FlightSegment DepDateTime="2012-10-27A12:35:00" Ticket="eTicket">
                <DepAirport LocationCode="DEL" Terminal=""/>
                <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80"/>
            </FlightSegment>
        </OrigDestinationOption>
    </OrigDestinationOptions>
</Information>
<Information xmlns="http://www.opentravel.org/OTA/2003/05">
<OrigDestinationOptions>
    <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-10-28A10:35:00" Ticket="eTicket">
            <DepAirport LocationCode="DEL" Terminal=""/>
            <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80"/>
        </FlightSegment>
    </OrigDestinationOption>
</OrigDestinationOptions>
</Information>
</Availability>

...产量...

<Availability>
  <Information xmlns="http://www.opentravel.org/OTA/2003/05">
    <OrigDestinationOptions>
      <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-10-27A12:35:00" Ticket="eTicket">
          <DepAirport LocationCode="DEL" Terminal="" />
          <BookingClassAvail ResBookDesigCode="T" ResBookDesigQuantity="180" />
          <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80" />
        </FlightSegment>
      </OrigDestinationOption>
    </OrigDestinationOptions>
  </Information>
  <Information xmlns="http://www.opentravel.org/OTA/2003/05">
    <OrigDestinationOptions>
      <OrigDestinationOption>
        <FlightSegment DepDateTime="2012-10-28A10:35:00" Ticket="eTicket">
          <DepAirport LocationCode="DEL" Terminal="" />
          <BookingClassAvail ResBookDesigCode="B" ResBookDesigQuantity="80" />
        </FlightSegment>
      </OrigDestinationOption>
    </OrigDestinationOptions>
  </Information>
</Availability>

笔记

  1. 分组键是父 OrigDestinationOptions 节点和航班起飞时间的乘积。
  2. 假设每个 OrigDestinationOption 恰好包含一个 FlightSegment。
  3. 假设当要合并同一组中的 FlightSegments 时,需要保留的组中所有成员的唯一不同信息是 BookingClassAvail 元素。
  4. 这不是一个超通用的解决方案,但考虑到问题表述中缺乏定义,这是合理的。如果您需要更通用的解决方案,则需要更精确地了解输入结构和转换规则。
于 2012-10-15T13:19:19.840 回答