Search code examples
orbeonxforms

xforms:decimal to show only one precision value


I have a simple issue. I am calculating the latest version from the list of revisions.

        <xforms:instance id="history">
            <metaData>
                <latestVersion></latestVersion>
                <History>
                    <Revision>
                        <Date>01/02/2011</Date>
                        <Comments>Mino Issues Fixed</Comments>
                        <Version>0.4</Version>
                    </Revision>
                    <Revision>
                        <Date>17/02/2011</Date>
                        <Comments>Minor issues fixed</Comments>
                        <Version>2.1</Version>
                    </Revision>
                    <Revision>
                        <Date>22/03/2011</Date>
                        <Comments>Cosmetic Defects Fixed</Comments>
                        <Version>2.2</Version>
                    </Revision>
                    <Revision>
                        <Date>06/04/2011</Date>
                        <Comments>minor issues fixed</Comments>
                        <Version>2.3</Version>
                    </Revision>
                    <Revision>
                        <Date>20/04/2011</Date>
                        <Comments>minor issues fixed</Comments>
                        <Version>2.4</Version>
                    </Revision>
                    <Revision>
                        <Date>22/04/2011</Date>
                        <Comments>Small build</Comments>
                        <Version>3.0</Version>
                    </Revision>
                </History>
            </metaData>
        </xforms:instance>

        <xforms:bind nodeset="instance('history')/latestVersion" 
            type="xforms:decimal"
            calculate="max(instance('history')/History/Revision/Version/number())" />

When i output latestVersion, it shows as 3.00. If i remove the type in bind definition it shows as 3. How can i show it as 3.0


Solution

  • There are three things to consider:

    • what types of values you are working with
    • what format the value stored into latestVersion is
    • how do you format the value for presentation to the user

    First, since your version numbers are decimals, you should consistently treat them as decimals. So you should not use the number() function, which always return an xs:double. You should rewrite the expression as:

    max(instance('history')/History/Revision/Version/xs:decimal(.))
    

    Second, when doing this, the value stored into latestVersion will be guaranteed to be in the xs:decimal format. Here, it will be 3.

    Third, how do you present that value to the user? You must make sure it is formatted properly. If you write:

    <xforms:output ref="latestVersion"/>
    

    The xforms:output looks at the type of the value, notices it is a decimal type, and formats it according to a default format for decimal types, see properties-xforms.xml.

    The default format for decimal types is:

    format-number(xs:decimal(.),'###,###,###,##0.00')
    

    If you want a decimal format, you can override the property in properties-local.xml, or use something like @grtjn suggested:

    <xforms:output value="format-number(instance('history')/latestVersion, '#.0')"/>
    

    Or:

    <xforms:output value="format-number(instance('history')/latestVersion, '#,###.0')"/>