Xsl number()函数不允许零

时间:2019-11-06 12:47:12

标签: xml xslt xslt-1.0 apache-fop

我正在使用xsl:if通过在表列-第2列中使用数字功能来检查值是否为数字,但不允许任何零。 当我通过删除xsl进行检查时:如果我在列单元格中获得0,则输出中不存在0。

我的XSL

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:template match="data">
            <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="simple"
                        page-height="8.5in" page-width="11in" margin-top=".5in"
                        margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
                        <fo:region-body margin-top="2cm" margin-bottom="2cm" />
                        <fo:region-before extent="2cm" overflow="hidden" />
                        <fo:region-after extent="1cm" overflow="hidden" />
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="simple"
                    initial-page-number="1">
                    <fo:static-content flow-name="xsl-region-before">
                        <fo:block font-size="13.0pt" font-family="serif"
                            padding-after="2.0pt" space-before="4.0pt" text-align="center"
                            border-bottom-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>PDF Test</xsl:text>
                        </fo:block>
                    </fo:static-content>
                    <fo:static-content flow-name="xsl-region-after">
                        <fo:block font-size="12.0pt" font-family="sans-serif"
                            padding-after="2.0pt" space-before="2.0pt" text-align="center"
                            border-top-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>Page</xsl:text>
                            <fo:page-number />
                        </fo:block>
                    </fo:static-content>
                    <fo:flow flow-name="xsl-region-body">
                        <xsl:apply-templates select="data-body" />
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
        <xsl:template match="data-body">
            <fo:block text-align="center">
                <fo:table table-layout="fixed" width="100%"
                    border-style="dashed">
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-header>
                        <xsl:apply-templates select="table-header" />
                    </fo:table-header>
                    <fo:table-body>
                        <xsl:apply-templates select="table-data" />
                    </fo:table-body>
                </fo:table>
            </fo:block>
        </xsl:template>
        <xsl:template match="table-header">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-two"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
        <xsl:template match="table-data">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:if test="number(column-two)">
                        <xsl:value-of select="format-number(translate(column-two, ',','.'), '#,###.##')"></xsl:value-of>
                        </xsl:if>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
    </xsl:stylesheet>

我的XML

<?xml version="1.0" encoding="UTF-8"?>
<data>
<data-body>
    <table-header>
        <column-one>Column One</column-one>
        <column-two>Column Two</column-two>
        <column-three>Column Three</column-three>
    </table-header>
    <table-data>
        <column-one>One</column-one>
        <column-two>5000</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>5000</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>0</column-two>
        <column-three>Three</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>0</column-two>
        <column-three>Four</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>2000</column-two>
        <column-three>Four</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>1234</column-two>
        <column-three>Five</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>5666</column-two>
        <column-three>Five</column-three>
    </table-data>
    <table-data>
        <column-one>One</column-one>
        <column-two>5666</column-two>
        <column-three>Five</column-three>
    </table-data>
     </data-body>
</data>

由于如果存在任何非数字字符,我需要在单元格中打印空白块,所以我在xsl:if中使用了此数字功能,以检查单元格值是否为数字以应用逗号分隔符。因此,如果我删除了xsl:if测试,则输出为0(如果猜测错误,请更正我)。我先使用xsl文件和xml文件使用apache fop创建PDF,在此先感谢。

1 个答案:

答案 0 :(得分:0)

测试的问题不是number()函数。 number()函数可将零正确转换为数字。但是,数字零在转换为布尔值时将求值为false(),并且xsl:if必须为其test表达式返回布尔结果。

请尝试:

<xsl:if test="number(function_number)=number(function_number)">

之所以有效,是因为NaN is never equal to anything, not even to itself

相关问题