2013-01-18 77 views
2

我對XSLT比較新,並且遇到了一些相對簡單的問題。我在互聯網上搜索了2天,但無法克服這個問題的最後障礙。xsl插入計算的元素

這裏是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> 

任何幫助將不勝感激!

基因

回答

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> 
+0

謝謝Dimitre!我會試試這個。週末愉快。 – user1990731