嗨首先,我正在学习复杂的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" />
此外,如果任何信息不同,它应该作为单独的航段。
另请注意,出境航班上方的一个标签和入境航班的其他信息需要单独保存,只需要合并。
提前感谢一百万。
Sean我已经使用了你建议的解决方案,我遇到了一个错误:“无法从已加载的输入文档中删除空格。请将输入文档改为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。”
答案 0 :(得分: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>