gpt4 book ai didi

c# - 在 XSLT 中组合相同的子元素

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:52 26 4
gpt4 key购买 nike

您好,首先我正在学习复杂的 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 提供。”

最佳答案

此 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. 假定当要合并同一组中的 FlightSegment 时,需要保留的组中所有成员的唯一不同信息是 BookingClassAvail 元素。
  4. 这不是一个 super 通用的解决方案,但考虑到问题表述中缺乏定义,这是合理的。如果您需要更通用的解决方案,则需要更精确地了解输入结构和转换规则。

关于c# - 在 XSLT 中组合相同的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com