2011-08-29 39 views
1

我XSLT.I是新手我使用XSLT(XML到XML)。請檢查我的代碼 -在XSLT總額

輸入XML -

<FCRep> 
     <AirInfo> 
      <AdtTransFee>0.000</AdtTransFee> 
      <ChdTransFee>0.000</ChdTransFee> 
      <InfTransFee>0.000</InfTransFee> 
      <AutoCharges grandTotal="30.000"> 
      <Charge name="SAFI" operator="+" isSupplierFee="TRUE" ApplyOn="BaseFare"> 
       <Adult>5.000</Adult> 
       <Child>5.000</Child> 
       <Infant>5.000</Infant> 
       <DBAmount type="fixed" /> 
       <Total>15.000</Total> 
      </Charge> 
      <Charge name="APC" operator="+" isSupplierFee="TRUE" ApplyOn="BaseFare"> 
       <Adult>5.000</Adult> 
       <Child>5.000</Child> 
       <Infant>5.000</Infant> 
       <DBAmount type="fixed" /> 
       <Total>15.000</Total> 
      </Charge> 
      <Charge name="Supplier Fee" operator="+" isSupplierFee="True" ApplyOn="BaseFare+YQ"> 
       <Adult>0.000</Adult> 
       <Child>0.000</Child> 
       <Infant>0.000</Infant> 
       <DBAmount type="False">0</DBAmount> 
       <Total>0.000</Total> 
      </Charge> 
      </AutoCharges> 
     </AirInfo> 
    </FCRep> 

XSLT -

<xsl:output method="xml" indent="yes"/> 
    <xsl:variable name="root" select="FareRules" /> 
    <xsl:variable name="journey" select="FareRules/JournyType" /> 
    <xsl:variable name ="nOfADT" select="FareRules/noa" /> 
    <xsl:variable name ="nOfCHD" select="FareRules/noc" /> 
    <xsl:variable name ="nOfINF" select="FareRules/noi" /> 
    <xsl:variable name="searchType" select="FareRules/SearchType" /> 

    <xsl:template match="/"> 
    <FlightDetailRule> 

     <xsl:element name="AirInfo"> 

     <FareDetails> 
      <xsl:if test="$nOfADT > 0"> 
      <xsl:element name="PaxType"> 
       <xsl:attribute name="Type"> 
       <xsl:value-of select="'ADT'"/> 
       </xsl:attribute> 
       <NoOfPax> 
       <xsl:value-of select="$nOfADT"/> 
       </NoOfPax> 
       <BFare> 
       <xsl:value-of select="ABP"/> 
       </BFare> 
       <Tax> 
       <xsl:value-of select="ATax"/> 
       </Tax> 
       <Yq> 
       <xsl:value-of select="AdultYQ"/> 
       </Yq> 

       <xsl:element name="OthChg"> 
       <xsl:attribute name="ChgTotal"> 
       </xsl:attribute> 
       <xsl:for-each select="AutoCharges/Charge"> 
        <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
         <xsl:value-of select="@name"/> 
        </xsl:attribute> 
        <xsl:value-of select="Adult"/> 
        </xsl:element> 
       </xsl:for-each> 
       <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
        <xsl:value-of select="'TransFee'"/> 
        </xsl:attribute> 
        <xsl:value-of select="AdtTransFee"/> 
       </xsl:element> 
       </xsl:element> 
      </xsl:element> 
      </xsl:if> 

      <xsl:if test="$nOfCHD > 0"> 
      <xsl:element name="PaxType"> 
       <xsl:attribute name="Type"> 
       <xsl:value-of select="'CHD'"/> 
       </xsl:attribute> 
       <NoOfPax> 
       <xsl:value-of select="$nOfCHD"/> 
       </NoOfPax> 
       <BFare> 
       <xsl:value-of select="CBP"/> 
       </BFare> 
       <Tax> 
       <xsl:value-of select="CTax"/> 
       </Tax> 
       <Yq> 
       <xsl:value-of select="ChildYQ"/> 
       </Yq> 

       <xsl:element name="OthChg"> 
       <xsl:attribute name="ChgTotal"> 
       </xsl:attribute> 
       <xsl:for-each select="AutoCharges/Charge"> 
        <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
         <xsl:value-of select="@name"/> 
        </xsl:attribute> 
        <xsl:value-of select="Child"/> 
        </xsl:element> 
       </xsl:for-each> 
       <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
        <xsl:value-of select="'TransFee'"/> 
        </xsl:attribute> 
        <xsl:value-of select="ChdTransFee"/> 
       </xsl:element> 
       </xsl:element> 

      </xsl:element> 
      </xsl:if> 

      <xsl:if test="$nOfINF > 0"> 
      <xsl:element name="PaxType"> 
       <xsl:attribute name="Type"> 
       <xsl:value-of select="'INF'"/> 
       </xsl:attribute> 
       <NoOfPax> 
       <xsl:value-of select="$nOfINF"/> 
       </NoOfPax> 
       <BFare> 
       <xsl:value-of select="IBP"/> 
       </BFare> 
       <Tax> 
       <xsl:value-of select="ITax"/> 
       </Tax> 
       <Yq> 
       <xsl:value-of select="InfantYQ"/> 
       </Yq> 

       <xsl:element name="OthChg"> 
       <xsl:attribute name="ChgTotal"> 
       </xsl:attribute> 
       <xsl:for-each select="AutoCharges/Charge"> 
        <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
         <xsl:value-of select="@name"/> 
        </xsl:attribute> 
        <xsl:value-of select="Infant"/> 
        </xsl:element> 
       </xsl:for-each> 
       <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
        <xsl:value-of select="'TransFee'"/> 
        </xsl:attribute> 
        <xsl:value-of select="InfTransFee"/> 
       </xsl:element> 
       </xsl:element> 

      </xsl:element> 
      </xsl:if> 

     </FareDetails> 

     </xsl:element> 
    </FlightDetailRule> 
    </xsl:template> 

</xsl:stylesheet> 

輸出XML -

<FlightDetailRule> 
     <AirInfo> 
      <FareDetails> 
      <PaxType Type="ADT"> 
       <NoOfPax>1</NoOfPax> 
       <BFare>12.000</BFare> 
       <Tax>53.200</Tax> 
       <Yq>0.0000</Yq> 
       <OthChg ChgTotal=""> 
       <Chg NM="SAFI">5.000</Chg> 
       <Chg NM="APC">5.000</Chg> 
       <Chg NM="Supplier Fee">0.000</Chg> 
       <Chg NM="TransFee">0.000</Chg> 
       </OthChg> 
      </PaxType> 
      <PaxType Type="CHD"> 
       <NoOfPax>1</NoOfPax> 
       <BFare>12.000</BFare> 
       <Tax>53.200</Tax> 
       <Yq>0.0000</Yq> 
       <OthChg ChgTotal=""> 
       <Chg NM="SAFI">5.000</Chg> 
       <Chg NM="APC">5.000</Chg> 
       <Chg NM="Supplier Fee">0.000</Chg> 
       <Chg NM="TransFee">0.000</Chg> 
       </OthChg> 
      </PaxType> 
      <PaxType Type="INF"> 
       <NoOfPax>1</NoOfPax> 
       <BFare>7.000</BFare> 
       <Tax>10.300</Tax> 
       <Yq>0.0000</Yq> 
       <OthChg ChgTotal=""> 
       <Chg NM="SAFI">5.000</Chg> 
       <Chg NM="APC">5.000</Chg> 
       <Chg NM="Supplier Fee">0.000</Chg> 
       <Chg NM="TransFee">0.000</Chg> 
       </OthChg> 
      </PaxType> 
      </FareDetails> 
     </AirInfo> 
    </FlightDetailRule> 

我的輸出即將發生e,但我也想ChgTotal屬性OthChg節點。 請建議我如何做到這一點。

在此先感謝。

+0

你有空OthChg元素中的ChgTotal屬性... –

+0

是Kirill.that是我想要的。:) –

+0

ChgTotal是所有Chg節點值的總和。 –

回答

2

正是這裏要使用標準的XPath函數sum()這樣

您的代碼

   <xsl:element name="OthChg"> 
       <xsl:attribute name="ChgTotal"> 
       </xsl:attribute> 
       <xsl:for-each select="AutoCharges/Charge"> 
        <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
         <xsl:value-of select="@name"/> 
        </xsl:attribute> 
        <xsl:value-of select="Adult"/> 
        </xsl:element> 
       </xsl:for-each> 
       <xsl:element name="Chg"> 
        <xsl:attribute name="NM"> 
        <xsl:value-of select="'TransFee'"/> 
        </xsl:attribute> 
        <xsl:value-of select="AdtTransFee"/> 
       </xsl:element> 
       </xsl:element> 
      </xsl:element> 

正確的代碼

  <OthChg ChgTotal="{sum(AutoCharges/Charge/Adult)+AdtTransFee}"> 
       <xsl:for-each select="AutoCharges/Charge"> 
        <Chg NM="{@name}"> 
        <xsl:value-of select="Adult"/> 
        </Chg> 
       </xsl:for-each> 
       <Chg NM="TransFee"> 
        <xsl:value-of select="AdtTransFee"/> 
       </xsl:element> 
      </OthChg> 
+0

謝謝Dimitre .. :) –

+0

@Mohit庫馬爾:不客氣。 –

1

看看XPath number sum(node-set)的功能。