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

时间:2012-06-14 14:54:53

标签: xslt

我正在尝试使用标识/复制模式仅转换下面的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>

2 个答案:

答案 0 :(得分:1)

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

关键部分是<xsl:choose>元素:当前上下文节点是<Segment>元素(来自<xsl:for-each>循环迭代)。因此,条件<xsl:when>元素的计算结果为 true ,如果在当前元素之前,同一父元素中没有其他<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>

答案 1 :(得分: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>