Xml XSLT中的总金额
我是XSLT的新手。我正在使用XSLT(XML到XML)。请检查我的代码- 输入XML-Xml XSLT中的总金额,xml,xslt,xpath,Xml,Xslt,Xpath,我是XSLT的新手。我正在使用XSLT(XML到XML)。请检查我的代码- 输入XML- <FCRep> <AirInfo> <AdtTransFee>0.000</AdtTransFee> <ChdTransFee>0.000</ChdTransFee> <InfTransFee>0.000</InfTransFee>
<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>
0
0
0
5
5
5
15
5
5
5
15
0
0
0
0
0
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>
1.
12
53.200
0
5
5
0
0
1.
12
53.200
0
5
5
0
0
1.
7
10.300
0
5
5
0
0
我的输出很好,但我还需要OthChg节点中的ChgTotal属性。
请告诉我怎么做
提前谢谢 查看XPath
数字和(节点集)
函数。这里使用标准XPath函数是合适的,如下所示:
<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>
您的代码:
<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>
正确的代码:
<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>
您在OthChg元素中有空的ChgTotal属性…是的,Kirill。这就是我想要的。:)ChgTotal是所有Chg节点值的总和。我已经测试了您的样式表,并且(正如我之前所想的)它不会产生您显示的结果。这是显而易见的,因为样式表与输入文档中的任何元素都不匹配。那么,您提供了错误的输入,或者错误的样式表,或者什么?好问题,+1。有关详细的解决方案,请参见我的答案。