pyxb 的十进制数据类型丢失零

pyxb's decimal datatype loses zeros

我注意到 pyxb decimal 数据类型在呈现为 XML 时不保留尾随零。罪魁祸首是在 XsdLiteral 函数的以下行中调用 normalize(),在 binding/datatypes.py 的第 159 行:

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

(其中 value 是 Python 的 decimal 的实例)。这对我来说有点问题,因为我尝试与之交互的 Web 服务需要 X.000 形式的版本号,而 pyxb 将其截断为 X.0.

这是预期的行为吗?或某些标准要求?其他 XML 模式生成库是否也这样做?我现在的解决方案是改用 string,但如果不破坏任何内容,代码将很容易更改。

我没有使用 pyxb 的经验,但我的猜测是通常人们希望 XML 尽可能紧凑以保留内存,因此小数截断以保留字节。

这似乎不是使用小数的正常情况。我认为应该使用小数来存储数学数值,因此截断总是可能的。由于您的情况特殊,该模块可能不是为满足您的需求而设计的。

官方 PyXB 回复是 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.

同样在 decimal 本身的描述中:

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

服务提供商不符合要求提供尾随零。