1

我正在尝试使用身份/复制模式来仅转换下面的 Segments/Segment 部分并保留结构的其余部分。模板会是什么样子?我尝试了论坛中的一些示例以在 xslt:for-each SegmentFares/Segments/Segment 中应用模板,但我不知道如何将基本票价放在第一个 SegmentFare 中,并将其设置为零在剩余的后续部分中段。

输入 XML:

    <FareQuoteRS>
      <PaxFareQuotes>
        <item>
          <PTC>ADT</PTC>
          <TotalFare>
            <Currency>INR</Currency>
            <Fare>8403</Fare>
            <TotalBaseFare>3620</TotalBaseFare>
          </TotalFare>
          <SegmentFares>
            <SegmentFare>
              <Currency>INR</Currency>
              <BaseFare>3620</BaseFare>
              <Segments>
                <Segment>
                  <Origin>DEL</Origin>
                  <Destination>BOM</Destination>
                  <DeptrDate>2012-06-23</DeptrDate>
                  <DeptrTime>10:10</DeptrTime>
                  <ArrDate>2012-06-23</ArrDate>
                  <ArrTime>12:15</ArrTime>
                  <FareBasis>J</FareBasis>
                  <PromoCode></PromoCode>
                  <FltNum>109</FltNum>
                  <Airline>YY</Airline>
                  <BagAllow>20K</BagAllow>
                </Segment>
                <Segment>
                  <Origin>BOM</Origin>
                  <Destination>GOI</Destination>
                  <DeptrDate>2012-06-23</DeptrDate>
                  <DeptrTime>15:15</DeptrTime>
                  <ArrDate>2012-06-23</ArrDate>
                  <ArrTime>16:20</ArrTime>
                  <FareBasis>J</FareBasis>
                  <PromoCode></PromoCode>
                  <FltNum>248</FltNum>
                  <Airline>YY</Airline>
                  <BagAllow>20K</BagAllow>
                </Segment>
              </Segments>
            </SegmentFare>
          </SegmentFares>
          <Taxes>
            <Currency>INR</Currency>
            <TotalTaxes>4783</TotalTaxes>
            <item>
              <TaxCode>YQ</TaxCode>
              <TaxName>Fuel Surcharge</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>3150</TaxAmount>
            </item>
            <item>
              <TaxCode>WO</TaxCode>
              <TaxName>PSF</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>146</TaxAmount>
            </item>
            <item>
              <TaxCode>IN</TaxCode>
              <TaxName>UDF</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>745</TaxAmount>
            </item>
            <item>
              <TaxCode>JN</TaxCode>
              <TaxName>Service Tax</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>335</TaxAmount>
            </item>
            <item>
              <TaxCode>TF</TaxCode>
              <TaxName>Transaction Fee</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>407</TaxAmount>
            </item>
          </Taxes>
        </item>
      </PaxFareQuotes>
      <Status>Success</Status>
      <StatusRemark/>
    </FareQuoteRS>

输出 XML:

    <FareQuoteRS>
      <PaxFareQuotes>
        <item>
          <PTC>ADT</PTC>
          <TotalFare>
            <Currency>INR</Currency>
            <Fare>8403</Fare>
            <TotalBaseFare>3620</TotalBaseFare>
          </TotalFare>
          <SegmentFares>
          <!-- There is now one SegmentFare for each Segment in the input -->
          <!-- while the BaseFare is set to zero for the subsequent segments -->
            <SegmentFare>
              <Currency>INR</Currency>
              <BaseFare>3620</BaseFare>
              <Segments>
                <Segment>
                  <Origin>DEL</Origin>
                  <Destination>BOM</Destination>
                  <DeptrDate>2012-06-23</DeptrDate>
                  <DeptrTime>10:10</DeptrTime>
                  <ArrDate>2012-06-23</ArrDate>
                  <ArrTime>12:15</ArrTime>
                  <FareBasis>J</FareBasis>
                  <PromoCode></PromoCode>
                  <FltNum>109</FltNum>
                  <Airline>YY</Airline>
                  <BagAllow>20K</BagAllow>
                </Segment>
              </Segments>
            </SegmentFare>
            <SegmentFare>
              <Currency>INR</Currency>
              <BaseFare>0</BaseFare>
              <Segments>
                <Segment>
                  <Origin>BOM</Origin>
                  <Destination>GOI</Destination>
                  <DeptrDate>2012-06-23</DeptrDate>
                  <DeptrTime>15:15</DeptrTime>
                  <ArrDate>2012-06-23</ArrDate>
                  <ArrTime>16:20</ArrTime>
                  <FareBasis>J</FareBasis>
                  <PromoCode></PromoCode>
                  <FltNum>248</FltNum>
                  <Airline>YY</Airline>
                  <BagAllow>20K</BagAllow>
                </Segment>
              </Segments>
            </SegmentFare>
          </SegmentFares>
          <Taxes>
            <Currency>INR</Currency>
            <TotalTaxes>4783</TotalTaxes>
            <item>
              <TaxCode>YQ</TaxCode>
              <TaxName>Fuel Surcharge</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>3150</TaxAmount>
            </item>
            <item>
              <TaxCode>WO</TaxCode>
              <TaxName>PSF</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>146</TaxAmount>
            </item>
            <item>
              <TaxCode>IN</TaxCode>
              <TaxName>UDF</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>745</TaxAmount>
            </item>
            <item>
              <TaxCode>JN</TaxCode>
              <TaxName>Service Tax</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>335</TaxAmount>
            </item>
            <item>
              <TaxCode>TF</TaxCode>
              <TaxName>Transaction Fee</TaxName>
              <Currency>INR</Currency>
              <TaxAmount>407</TaxAmount>
            </item>
          </Taxes>
        </item>
      </PaxFareQuotes>
      <Status>Success</Status>
      <StatusRemark/>
    </FareQuoteRS>
4

2 回答 2

1

你的想法<xsl:for-each>很好;它可以如下所示完成。

关键部分是<xsl:choose>元素:当前上下文节点是一个<Segment>元素(来自<xsl:for-each>循环迭代)。因此,如果在当前元素之前,同一父元素中没有其他元素,则条件<xsl:when>元素的计算结果为真。<Segment>如果为true,则复制原始<BaseFare>值,否则<BaseFare>插入零值。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

    <xsl:template match="/FareQuoteRS/PaxFareQuotes/item/SegmentFares">
        <xsl:copy>
            <xsl:for-each select="SegmentFare/Segments/Segment">
                <SegmentFare>
                    <xsl:copy-of select="../../Currency"/>
                    <xsl:choose>
                        <xsl:when test="not(preceding-sibling::Segment)">
                            <xsl:copy-of select="../../BaseFare"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <BaseFare>0</BaseFare>
                        </xsl:otherwise>
                    </xsl:choose>
                    <Segments>
                        <xsl:copy>
                            <xsl:apply-templates select="node()|@*"/>
                        </xsl:copy>
                    </Segments>
                </SegmentFare>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
于 2012-06-14T15:18:41.463 回答
1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/><xsl:strip-space elements="*"/>

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

    <!-- build first SegmentFare -->
    <xsl:template match="SegmentFares/SegmentFare">
        <xsl:copy>
            <xsl:apply-templates select="Currency|BaseFare"/>
            <Segments>
                <!-- leave first Segment as is -->
                <xsl:apply-templates select="Segments/Segment[1]"/>
            </Segments>
        </xsl:copy>
        <!-- apply templates to any next Segment -->
        <xsl:apply-templates select="Segments/Segment[position()&gt;1]" mode="BaseFare0"/>
    </xsl:template>

    <!-- wrap single Segment with BaseFare 0 -->
    <xsl:template match="Segment" mode="BaseFare0">
        <SegmentFare>
            <xsl:copy-of select="ancestor::SegmentFare/Currency"/>
            <BaseFare>0</BaseFare>
            <Segments>
                <xsl:apply-templates select="."/>
            </Segments>
        </SegmentFare>
    </xsl:template>

</xsl:stylesheet>
于 2012-06-14T21:05:07.730 回答