得到错误:
无法将类型'string'隐式转换为'int'
在这段代码上:
<TotalInvoiceCost>
<xsl:value-of select="sum(//*[local-name()='InvoiceTotal'])" />
</TotalInvoiceCost>
然后尝试了这个:
<TotalInvoiceCost>
<xsl:value-of select="sum(number(//*[local-name()='InvoiceTotal']))" />
</TotalInvoiceCost>
但得到这个错误:
函数'sum()'的参数1无法转换为节点集。
使用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,这很好用:
总和(// * [本地名称()= “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>
那么,当我们遇到问题时,现实世界的程序通常比我们在网上发布的内容更复杂;我试着简化。该错误与我发布的代码无关。
对我来说,似乎最大的问题是Visual Studio中“测试图”的错误没有给出带错误的行号的任何提示。
<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”应该是上面的“公共字符串”。
我知道如何解决这些问题的唯一方法就是“分而治之”的方法。我制作了一个XSLT副本,用_Debug.xslt命名它,更改了BizTalk映射以改为使用它,并开始左右删除代码行直到错误消失。即使删除对c#的调用也没有删除错误,因此它必须在代码上运行.NET编译或语法检查。