2

我对 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>寻找具有相同值的 a。<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>

任何帮助将不胜感激!

基因

4

1 回答 1

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>
于 2013-01-18T22:26:04.287 回答