Search code examples
pythonxmlpyxb

pyxb's decimal datatype loses zeros


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.


Solution

  • 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.