如何解决如果我们使用子字符串函数
| 我想问一下如果我们使用子字符串函数如何获取总金额, 例如我有这个xml,<document>
<line id=\"0\">
<field id=\"0\"><![CDATA[AAAddd17aaass]]></field>
</line>
<line id=\"1\">
<field id=\"0\"><![CDATA[DDDaaa33sssaa]]></field>
</line>
</document>
我应该求和(substring(field [@id = \'0 \'],7,2))。然后,我尝试执行此操作,收到以下错误消息:当前项是\'NaN,类型为xs:string。 (我尝试使用数字功能,但无济于事)
请给我建议如何解决。
解决方法
在XSLT 1.0中,如果您不想自己编写递归模板,则可以使用FXSL中的
transform-and-sum
模板。
在此处查看如何使用它。
这是完整的转换:
<xsl:stylesheet version=\"1.0\"
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"
xmlns:f=\"http://fxsl.sf.net/\"
xmlns:func-transform=\"f:func-transform\"
exclude-result-prefixes=\"xsl f func-transform\"
>
<xsl:import href=\"transform-and-sum.xsl\"/>
<!-- to be applied on testTransform-and-sum.xml -->
<xsl:output method=\"text\"/>
<func-transform:func-transform/>
<xsl:template match=\"/\">
<xsl:call-template name=\"transform-and-sum\">
<xsl:with-param name=\"pFuncTransform\"
select=\"document(\'\')/*/func-transform:*[1]\"/>
<xsl:with-param name=\"pList\" select=\"/*/*/*\"/>
</xsl:call-template>
</xsl:template>
<xsl:template match=\"func-transform:*\" mode=\"f:FXSL\">
<xsl:param name=\"arg1\" select=\"0\"/>
<xsl:value-of select=\"number(substring($arg1,7,2))\"/>
</xsl:template>
</xsl:stylesheet>
当应用于提供的XML文档时:
<document>
<line id=\"0\">
<field id=\"0\"><![CDATA[AAAddd17aaass]]></field>
</line>
<line id=\"1\">
<field id=\"0\"><![CDATA[DDDaaa33sssaa]]></field>
</line>
</document>
所需的正确结果产生了:
50
,在XSLT 2.0中很容易:
sum(field[@id=\'0\']/number(substring(.,2)))
在XSLT 1.0中要困难得多:例如,参见http://www.velocityreviews.com/forums/t170401-sum-over-computed-value.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。