pyxb's decimal datatype loses zeros

时间:2015-07-08 15:37:25

标签: python xml pyxb

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.

2 个答案:

答案 0 :(得分:1)

我没有使用pyxb的经验,但我的猜测是,为了保留内存,通常会希望XML尽可能紧凑,因此十进制截断以保留字节。

使用小数似乎不是正常情况。我认为小数应该用于存储数学数值,因此截断总是可行的。由于您的案例非常出色,因此该模块可能并非旨在满足您的需求。

答案 1 :(得分:1)

官方PyXB回复是here,但来自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.

同样在十进制本身的描述中:

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

要求提供尾随零,服务提供商不一致。