2

I've noticed that the pyxb decimal datatype doesn't preserve trailing zeroes when it renders to XML. The culprit is a call to normalize() in the following line of the XsdLiteral function, in line 159 of binding/datatypes.py:

(sign, digits, exponent) = value.normalize().as_tuple()

(where value is an instance of Python's decimal). This is a bit of a problem for me because the web service I am trying to interact with requires version numbers of the form X.000 and pyxb is truncating that to X.0.

Is this expected behavior? or required by some standard? Do other XML schema-generating libraries do this as well? My solution right now is to use string instead, but the code would be easy to change if it doesn't break anything.

Eli Rose
  • 6,788
  • 8
  • 35
  • 55

2 Answers2

1

I have no experience with pyxb, but my guess is that in general one wants XML to be as compact as possible in order to preserve memory, and so decimals truncate to preserve bytes.

This does not seem to be a normal case to use a decimal in. I gather that the decimal should be used to store mathematical-numerical values, and therefore truncation is always possible. Since your case is exceptional, the module was probably not designed to do what you are wanting.

bcdan
  • 1,438
  • 1
  • 12
  • 25
  • Thanks. It does seem to me as if it should be stored as a string. – Eli Rose Jul 08 '15 at 17:45
  • Turns out this schema is coming from the [Open Travel Association](http://www.opentravel.org/2013A/OTA_CommonTypes.xsd) -- ctrl+F 'name="Version" type="xs:decimal"' in there -- and as such cannot be changed. – Eli Rose Jul 08 '15 at 19:54
1

The official PyXB response is here, but from the description of the canonical representation of an xs:decimal value:

Leading and trailing zeroes are prohibited subject to the following:
there must be at least one digit to the right and to the left of the
decimal point which may be a zero.

Also in the description of decimal itself:

Precision is not reflected in this value space; the number 2.0 is not
distinct from the number 2.00.

The service provider is nonconformant by requiring the trailing zeros be provided.

pabigot
  • 989
  • 7
  • 8