0

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

Jayy
  • 2,368
  • 4
  • 24
  • 35

2 Answers2

4

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')"/>
ebruchez
  • 7,760
  • 6
  • 29
  • 41
  • What is the difference between xs:decimal and xs:double – Jayy Nov 05 '12 at 12:47
  • Also it seems there is no difference between number(.) and xs:decimal(.). When i run number(3.1) in xpath sandbox, it returns 3.1 as result and same result with decimal(). – Jayy Nov 05 '12 at 12:50
  • For the difference between decimal and double, see other StackOverflow answers, like [this one](http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c). – ebruchez Nov 05 '12 at 16:51
1

You can tweak presentation by using <xforms:output> with a value attribute instead of a ref or bind. The value of that attribute is an expression, and could contain a call to format-number(). For example:

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

Note: support for format-number may depend on implementation, but I thought Orbeon supports it.

grtjn
  • 20,254
  • 1
  • 24
  • 35