2012-06-14 4 views
1

ID/복사 패턴을 사용하여 아래의 세그먼트/세그먼트 부분 만 변환하고 나머지 구조는 유지하려고합니다. 템플릿은 어떻게 생겼을까요? 나는 xslt : for-each SegmentFares/Segments/Segment에 템플릿을 적용하기 위해 포럼에서 몇 가지 예제를 시도했지만 첫 번째 SegmentFare에 기본 요금을 넣는 방법을 알지 못하고 나머지 SegmentFare의 후속 항목을 0으로 설정하는 방법을 모르겠습니다. 세그먼트.XSLT를 사용하여 XML을 변환하는 데 도움이 필요합니다.

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

답변

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

멋진! 나는 항상 새로운 것을 배웁니다. 둘 다 대단히 감사합니다. 두 대답은 모두 놀랍습니다. 둘 다 올바른 것으로 표시 할 수는 없지만 하나를 표시하고 두 가지를 모두 유용하게 설정합니다. – Surge

1

<xsl:for-each>에 대한 당신의 생각 좋았다; 아래와 같이 할 수 있습니다.

중요한 부분은 <xsl:choose> 요소입니다. 현재 컨텍스트 노드는 <Segment> 요소 (<xsl:for-each> 루프 반복)입니다. 따라서 조건 <xsl:when>은 으로 계산됩니다. 현재 요소 앞에있는 경우 동일한 부모 요소에 다른 <Segment> 요소가 없습니다. 이것이 이면 원래의 <BaseFare> 값이 복사되고, 그렇지 않으면 <BaseFare>이 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

멋진! 나는 항상 새로운 것을 배웁니다. 둘 다 대단히 감사합니다. – Surge

관련 문제