BizTalk 映射:xslt 1.0 总和 "Cannot implicitly convert type 'string' to 'int'"

BizTalk Map: xslt 1.0 sum "Cannot implicitly convert type 'string' to 'int'"

获取错误:

Cannot implicitly convert type 'string' to 'int'

在此代码上:

<TotalInvoiceCost>
    <xsl:value-of select="sum(//*[local-name()='InvoiceTotal'])" />
</TotalInvoiceCost>

然后试了一下:

   <TotalInvoiceCost>
      <xsl:value-of select="sum(number(//*[local-name()='InvoiceTotal']))" />
   </TotalInvoiceCost>

但出现此错误:

Argument 1 of function 'sum()' cannot be converted to a node-set.

Cut Down Sample Data with Structure(所有值都是数字):

<TAR210 xmlns="demo">
    <DummyHeaderGroup xmlns=""/>
    <Invoice xmlns="">
        <Level1>
            <InvoiceTotal>1075</InvoiceTotal>
        </Level1>
        <Level1>
            <InvoiceTotal>595</InvoiceTotal>
        </Level1>
    </Invoice>
</TAR210>

http://www.xpathtester.com/xpath 中,这很好用:

sum(//*[local-name()="InvoiceTotal"])

上下文中的 XSLT 示例:

<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var"
  exclude-result-prefixes="msxsl var userCSharp" version="1.0"
  xmlns:ns0="demo"
  xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">

  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />

  <xsl:template match="/">
    <ns0:TAR210>
      <DummyHeaderGroup>
        <Level0>
          <TotalInvoiceCost>
            <xsl:value-of select="sum(//*[local-name()='InvoiceTotal'])" />
          </TotalInvoiceCost>
        </Level0>
      </DummyHeaderGroup>
    </ns0:TAR210>
  </xsl:template>

</xsl:stylesheet>

嗯,当我们遇到问题时,现实世界的程序通常比我们 post 在网络上更复杂;我试图简化。该错误与我 post 编写的代码无关。

对我来说,最大的问题似乎是 Visual Studio 中的 "Test Map" 中的错误没有给出错误行号的任何提示。

<Level0>
  <BatchNumberLeaveBlank></BatchNumberLeaveBlank>
  <InvoiceSendDateCCYYMM>
    <!-- Get current CCYYMM -->
          <!-- Current-DateTime doesn't exist in XSLT 1.0, had to use C# 
          <xsl:value-of  select="concat(substring(current-dateTime(),1,4),substring(current-dateTime(),6,2))"/>
          --> 
          <xsl:value-of  select="userCSharp:GetDateCCYYMM()"/>
  </InvoiceSendDateCCYYMM>
  <DefaultValueN>N</DefaultValueN>
  <TotalInvoiceCost>...

然后我得到了下面的 C# 代码:

  <msxsl:script language="C#" implements-prefix="userCSharp">
<![CDATA[ int lineCount = 0;

public int GetDateCCYYMM()
   {
       return DateTime.Now.ToString("yyyyMMdd");
   }

  ]]> 
  </msxsl:script>

显然,"public int" 应该是上面的 "public string"。

我知道如何解决这些问题的唯一方法是 "divide and conquer" 方法。 我制作了 XSLT 的副本,将其命名为 _Debug.xslt,更改了 BizTalk 映射以改为使用它,并开始左右删除代码行,直到错误消失。即使删除对 c# 的调用也没有消除错误,因此它一定是 运行 对代码进行的 .NET 编译或语法检查。