XSLT中的总金额

时间:2011-08-29 07:04:04

标签: xml xslt xpath

我是XSLT的新手。我正在使用XSLT(XML到XML)。请检查我的代码 -

输入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>

我的输出正常,但我还想在 OthChg 节点中使用 ChgTotal 属性。 请建议我如何做到这一点。

提前致谢。

2 个答案:

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

答案 1 :(得分:1)

查看XPath number sum(node-set)函数。