如果我们使用子字符串函数

问题描述

| 我想问一下如果我们使用子字符串函数如何获取总金额, 例如我有这个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     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...