我使用XSLT将数据的结果集从数据库转换为格式良好的表,包括计算总计的算术。
结果集作为XML文档(通过SOAP)提供给我,但我没有能力改变用于生成它的查询。
问题:当值显示为零时,此查询将返回null / empty值到XML结果集中。为了使算法工作,我需要将这些空值发生时转换为零。
我尝试了各种方法,包括一个变量(见下文)但没有成功 - 在这种情况下,我得到:
Cannot covert string "" to a double
我对预处理数据的方法有点限制:有没有办法用XSLT实现这个目标?
输入:
<DataResultSet xmlns="urn:...dataservice" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:axis2ns531="urn:...dataservice">
<rows>
<row>
<A_PLAN>438</A_PLAN>
<A_ACTUAL>358</A_ACTUAL>
<B_PLAN />
<B_ACTUAL />
</row>
</rows>
</DataResultSet>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:jdbc="urn:...dataservice" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="html"/>
<xsl:variable name = "A_PLAN" as="xs:double">
<xsl:choose>
<xsl:when test = "jdbc:A_PLAN = ''" ><xsl:text>0</xsl:text></xsl:when>
<xsl:otherwise><xsl:value-of select="jdbc:A_PLAN" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="A_ACTUAL" as="xs:double">
<xsl:choose>
<xsl:when test = "jdbc:A_ACTUAL = ''" ><xsl:text>0</xsl:text></xsl:when>
<xsl:otherwise><xsl:value-of select="jdbc:A_ACTUAL" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="B_PLAN" as="xs:double">
<xsl:choose>
<xsl:when test = "jdbc:B_PLAN = ''" ><xsl:text>0</xsl:text></xsl:when>
<xsl:otherwise><xsl:value-of select="jdbc:B_PLAN" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="B_ACTUAL" as="xs:double">
<xsl:choose>
<xsl:when test = "jdbc:B_ACTUAL = ''" ><xsl:text>0</xsl:text></xsl:when>
<xsl:otherwise><xsl:value-of select="jdbc:B_ACTUAL" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="jdbc:row">|r|A|c|XXX|c|<xsl:value-of select="$A_PLAN"/>|c|<xsl:value-of select="$A_ACTUAL"/>|c|<xsl:value-of select="$A_ACTUAL - $A_PLAN"/>|r|B|c|XXX|c|<xsl:value-of select="$B_PLAN"/>|c|<xsl:value-of select="$B_ACTUAL"/>|c|<xsl:value-of select="$B_ACTUAL - $B_PLAN"/>|r|XXX|c|TTL|c|<xsl:value-of select="$A_PLAN + $B_PLAN"/>|c|<xsl:value-of select="$A_ACTUAL + $B_ACTUAL"/>|c|<xsl:value-of select="($A_ACTUAL - $A_PLAN) + ($B_ACTUAL - $B_PLAN)"/>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:4)
我会写
<xsl:variable name = "A_PLAN" as="xs:double">
<xsl:choose>
<xsl:when test = "jdbc:A_PLAN = ''" ><xsl:text>0</xsl:text></xsl:when>
<xsl:otherwise><xsl:value-of select="jdbc:A_PLAN" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
作为
<xsl:variable name = "A_PLAN" as="xs:double"
select="if (//jdbc:A_PLAN castable as xs:double) then xs:double(//jdbc:A_PLAN) else 0"/>
所以我已经更正了使用//jdbc:A_PLAN
的路径,因为您似乎想要使用全局变量,然后您需要在文档中向下搜索并使用castable as
来检查值。 / p>
当然,如果你有几个使用全局变量的row
个元素没有意义,那么我会将代码移到模板中,例如
<xsl:template match="jdbc:row">
<xsl:variable name = "A_PLAN" as="xs:double"
select="if (jdbc:A_PLAN castable as xs:double) then xs:double(jdbc:A_PLAN) else 0"/>