2011-08-29 61 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)的功能。