需要帮助使用XSLT转换我的XML吗

需要帮助使用XSLT转换我的XML吗,xslt,Xslt,我尝试使用标识/复制模式仅转换下面的段/段部分,并保留结构的其余部分。模板是什么样子的?我尝试了论坛中的一些示例,以在xslt中应用模板:对于每个分段票价/分段/分段,但我不知道如何在第一个分段中设置基本票价,并在其余分段中的后续分段中将其设置为零 输入XML: <FareQuoteRS> <PaxFareQuotes> <item> <PTC>ADT</PTC>

我尝试使用标识/复制模式仅转换下面的段/段部分,并保留结构的其余部分。模板是什么样子的?我尝试了论坛中的一些示例,以在xslt中应用模板:对于每个分段票价/分段/分段,但我不知道如何在第一个分段中设置基本票价,并在其余分段中的后续分段中将其设置为零

输入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>

ADT
印度卢比
8403
3620
印度卢比
3620
德尔
物料清单
2012-06-23
10:10
2012-06-23
12:15
J
109
YY
20K
物料清单
戈伊
2012-06-23
15:15
2012-06-23
16:20
J
248
YY
20K
印度卢比
4783
YQ
燃油附加费
印度卢比
3150
沃
PSF
印度卢比
146
在里面
自由民主党
印度卢比
745
JN
服务税
印度卢比
335
TF
交易费
印度卢比
407
成功
输出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>

ADT
印度卢比
8403
3620
印度卢比
3620
德尔
物料清单
2012-06-23
10:10
2012-06-23
12:15
J
109
YY
20K
印度卢比
0
物料清单
戈伊
2012-06-23
15:15
2012-06-23
16:20
J
248
YY
20K
印度卢比
4783
YQ
燃油附加费
印度卢比
3150
沃
PSF
印度卢比
146
在里面
自由民主党
印度卢比
745
JN
服务税
印度卢比
335
TF
交易费
印度卢比
407
成功
你对
的想法很好;可以按如下所示进行操作

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

<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>

0

0

太棒了!我总是能从社区学到新东西。非常感谢你们两位,太好了!我总是能从社区学到新东西。非常感谢你们两位。这两个答案都很惊人-无法将两者都标记为正确..因此我将其中一个标记为正确,并将两者设置为有用。
<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>