xsl插入计算元素

时间:2013-01-18 19:57:51

标签: xslt-2.0

我对XSLT相对较新,并且遇到了一些应该相对简单的问题。我在互联网上搜索了2天,但无法克服这个问题的最后一个障碍。

以下是我想要转换的XML的简单版本:

<Payment xmlns="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema.xsd ">
<Transaction>
    <POSellerName>DAF KNITWEARS LTD. (PG)</POSellerName>
    <PayeeName>883456789</PayeeName>
    <CurrencyCode>USD</CurrencyCode>
    <WTLCNumber>O0910122</WTLCNumber>
    <VendorAppNumber>6031</VendorAppNumber>
    <DocumentNumber>BEAI12000094</DocumentNumber>
    <PODetail>
        <PONumber>0887537</PONumber>
        <QuantityShipped>2550</QuantityShipped>
        <UnitOfMeasure>PCS</UnitOfMeasure>
        <CurrencyCode>USD</CurrencyCode>
        <AmountBilled>13226.37</AmountBilled>
    </PODetail>
    <PODetail>
        <PONumber>0887567</PONumber>
        <QuantityShipped>150</QuantityShipped>
        <UnitOfMeasure>PCS</UnitOfMeasure>
        <CurrencyCode>USD</CurrencyCode>
        <AmountBilled>873</AmountBilled>
    </PODetail>
  <ChargeBackDetail>
    <PONumber>0887567</PONumber>
    <CurrencyCode>USD</CurrencyCode>
    <ChargeBackAllocationAmount>-105</ChargeBackAllocationAmount>
    <ChargeBackReferenceNumber>CB0872364C</ChargeBackReferenceNumber>
    <UPC/>
  </ChargeBackDetail>
  <ChargeBackDetail>
    <PONumber>0872355</PONumber>
    <CurrencyCode>USD</CurrencyCode>
    <ChargeBackAllocationAmount>-105</ChargeBackAllocationAmount>
    <ChargeBackReferenceNumber>CB0872355A</ChargeBackReferenceNumber>
    <UPC/>
  </ChargeBackDetail>
</Transaction>
</Payment>

我想在名为

的每个<PODetail>的末尾插入一个元素
<AmountPaid>.

因此,我处理每个<PODetail>并在各自的元素中查找具有相同值的<ChargeBackDetail>。如果找到,该元素的值为/ PODetail / Amountbilled - / ChargeBackDetail / ChargeBackAllocationAmount,否则该值将等于/ PODetail / Amountbilled。下面是他对PONumber元素的期望结果。

  <PODetail>
    <PONumber>0887537</PONumber>
    <QuantityShipped>2550</QuantityShipped>
    <UnitOfMeasure>PCS</UnitOfMeasure>
    <CurrencyCode>USD</CurrencyCode>
    <AmountBilled>13226.37</AmountBilled>
    <AmountPaid>13226.37</AmountPaid>
</PODetail>
<PODetail>
    <PONumber>0887567</PONumber>
    <QuantityShipped>150</QuantityShipped>
    <UnitOfMeasure>PCS</UnitOfMeasure>
    <CurrencyCode>USD</CurrencyCode>
    <AmountBilled>873</AmountBilled>
    <AmountPaid>768</AmountPaid>
</PODetail>

这是我的XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:pf="http://www.WellsFargo.com/CIB/GIFTS/TradeERP/PaymentFileSchema">

<xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="pf:PODetail">
 <xsl:copy>
 <xsl:apply-templates select="@* | node()"/>

<xsl:variable name="poNumber" select="pf:PONumber"/>
<xsl:variable name="poAmountBilled" select="number(pf:AmountBilled)"/>

<xsl:for-each select="//pf:Transaction/pf:ChargeBackDetail">
    <xsl:variable name="poNumber" select="pf:PODetail/pf:PONumber"/>
    <xsl:variable name="cbPONumber" select="./pf:ChargeBackDetail/pf:PONumber"/>
    <xsl:variable name="cbAmount" select="number(./pf:ChargeBackDetail/pf:ChargeBackAllocationAmount)"/>

        <xsl:if test='$poNumber = $cbPONumber'> 
            <AmountPaid><xsl:number value="$poAmountBilled - $cbAmount"/></AmountPaid>
        </xsl:if>

</xsl:for-each>

 <xsl:if test ="not(pf:AmountPaid)">
<AmountPaid><xsl:number value="$poAmountBilled"/> </AmountPaid> 
 </xsl:if> 

</xsl:copy>
</xsl:template>

 </xsl:stylesheet>

非常感谢任何帮助!

基因

1 个答案:

答案 0 :(得分:0)

此转化

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema" exclude-result-prefixes="x">
    <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="x:PODetail">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
   <xsl:element name="AmountPaid"
                namespace="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema">
     <xsl:sequence select=
     "sum((x:AmountBilled,
          ../x:ChargeBackDetail[x:PONumber eq current()/x:PONumber]
                          /x:ChargeBackAllocationAmount))"/>
   </xsl:element>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<Payment xmlns="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema.xsd ">
<Transaction>
    <POSellerName>DAF KNITWEARS LTD. (PG)</POSellerName>
    <PayeeName>883456789</PayeeName>
    <CurrencyCode>USD</CurrencyCode>
    <WTLCNumber>O0910122</WTLCNumber>
    <VendorAppNumber>6031</VendorAppNumber>
    <DocumentNumber>BEAI12000094</DocumentNumber>
    <PODetail>
        <PONumber>0887537</PONumber>
        <QuantityShipped>2550</QuantityShipped>
        <UnitOfMeasure>PCS</UnitOfMeasure>
        <CurrencyCode>USD</CurrencyCode>
        <AmountBilled>13226.37</AmountBilled>
    </PODetail>
    <PODetail>
        <PONumber>0887567</PONumber>
        <QuantityShipped>150</QuantityShipped>
        <UnitOfMeasure>PCS</UnitOfMeasure>
        <CurrencyCode>USD</CurrencyCode>
        <AmountBilled>873</AmountBilled>
    </PODetail>
  <ChargeBackDetail>
    <PONumber>0887567</PONumber>
    <CurrencyCode>USD</CurrencyCode>
    <ChargeBackAllocationAmount>-105</ChargeBackAllocationAmount>
    <ChargeBackReferenceNumber>CB0872364C</ChargeBackReferenceNumber>
    <UPC/>
  </ChargeBackDetail>
  <ChargeBackDetail>
    <PONumber>0872355</PONumber>
    <CurrencyCode>USD</CurrencyCode>
    <ChargeBackAllocationAmount>-105</ChargeBackAllocationAmount>
    <ChargeBackReferenceNumber>CB0872355A</ChargeBackReferenceNumber>
    <UPC/>
  </ChargeBackDetail>
</Transaction>
</Payment>

会产生想要的正确结果:

<Payment xmlns="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.xxxxxx.com/CIB/GIFTS/PaymentFileSchema.xsd ">
   <Transaction>
      <POSellerName>DAF KNITWEARS LTD. (PG)</POSellerName>
      <PayeeName>883456789</PayeeName>
      <CurrencyCode>USD</CurrencyCode>
      <WTLCNumber>O0910122</WTLCNumber>
      <VendorAppNumber>6031</VendorAppNumber>
      <DocumentNumber>BEAI12000094</DocumentNumber>
      <PODetail>
        <PONumber>0887537</PONumber>
        <QuantityShipped>2550</QuantityShipped>
        <UnitOfMeasure>PCS</UnitOfMeasure>
        <CurrencyCode>USD</CurrencyCode>
        <AmountBilled>13226.37</AmountBilled>
         <AmountPaid>13226.37</AmountPaid>
      </PODetail>
      <PODetail>
        <PONumber>0887567</PONumber>
        <QuantityShipped>150</QuantityShipped>
        <UnitOfMeasure>PCS</UnitOfMeasure>
        <CurrencyCode>USD</CurrencyCode>
        <AmountBilled>873</AmountBilled>
         <AmountPaid>768</AmountPaid>
      </PODetail>
      <ChargeBackDetail>
         <PONumber>0887567</PONumber>
         <CurrencyCode>USD</CurrencyCode>
         <ChargeBackAllocationAmount>-105</ChargeBackAllocationAmount>
         <ChargeBackReferenceNumber>CB0872364C</ChargeBackReferenceNumber>
         <UPC/>
      </ChargeBackDetail>
      <ChargeBackDetail>
         <PONumber>0872355</PONumber>
         <CurrencyCode>USD</CurrencyCode>
         <ChargeBackAllocationAmount>-105</ChargeBackAllocationAmount>
         <ChargeBackReferenceNumber>CB0872355A</ChargeBackReferenceNumber>
         <UPC/>
      </ChargeBackDetail>
   </Transaction>
</Payment>