1

我正在与 google checkout api 集成,它们的所有属性都在其属性值中包含连字符。因此,要创建一个对订单收费的请求,我需要发送一个如下所示的 xml 帖子:

<?xml version="1.0" encoding="UTF-8"?>
<charge-and-ship-order xmlns="http://checkout.google.com/schema/2" google-order-number="6014423719">
    <amount currency="USD">335.55</amount>
</charge-and-ship-order>

我无法使用属性“google-order-number”构建该 xml。如果我想创建一个空节点,则以下代码有效:

>>> xml=XMLBuilder()
>>> xml << ('charge-and-ship-order, {'xmlns':'xxx','google-order-number':'3433'})
>>> str(xml)
>>> <charge-and-ship-order google-order-number="3433" xmlns="xxx" /> 

但是,如果我尝试使用记录的方式为金额创建子节点:

>>> xml=XMLBuilder()
>>> with xml('charge-and-ship-order', xmlns='xxx', google-order-number='3433'}):
>>>     with xml('amount', currency="USD"):
>>>         xml << '4.54'

我收到一条错误消息:

SyntaxError: keyword can't be an expression

我也试过:

>>> xml=XMLBuilder()
>>> with xml('charge-and-ship-order', {'xmlns':'xxx', 'google-order-number':'3433'}):
>>>     with xml << 'test'

我在 XMLBuilder 库中得到回溯,说

    File "/xmlbuilder/xmlbuilder/__init__.py", line 102, in __call__
    x(*dt,**mp)
    File "/xmlbuilder/xmlbuilder/__init__.py", line 36, in __call__
    text = "".join(dt)
    TypeError: sequence item 0: expected string, dict found

任何想法如何使用这样的属性?我正在使用位于 http://pypi.python.org/pypi/xmlbuilder的 XMLBuilder 库

4

1 回答 1

0

您可以像这样在字典中传递属性:

function_call(**{'weird-named-key': 'value'})
于 2011-08-13T22:32:39.527 回答