在 Python 中连接两个 XML 模板
Concatenate two XML templates in Python
我有两个 XML 模板需要在满足某些条件时添加。我连接似乎没有正确处理它们。
template1 = \
'''
<ParentTag>
<orders orderid="%s">
<unitprice>%s</unitprice>
<quantity>%s</quantity>
</orders>
</ParentTag>
'''
template2 = \
'''
<details>
<productid>%s</productid>
<productname>%s</productname>
</details>
我的 objective 是要获取一个 xml 文件,如下所示:
<ParentTag>
<orders orderid="%s">
<unitprice>%s</unitprice>
<quantity>%s</quantity>
</orders>
<details>
<productid>%s</productid>
<productname>%s</productname>
</details>
</ParentTag>
python 代码片段:
output = ""
if (condition is met):
output += output % template1(orderid, unitprice, quantity)
else:
output += <template1 + template2>
如何连接 Python 中的两个模板。
一种(非正统的)方法是这样的:
template1 = \
'''
<ParentTag>
<orders orderid="%s">
<unitprice>%s</unitprice>
<quantity>%s</quantity>
</orders>%s
</ParentTag>
'''
template2 = \
'''
<details>
<productid>%s</productid>
<productname>%s</productname>
</details>
'''
代码:
output = ""
if (condition is met):
output += output % template1(orderid, unitprice, quantity, "")
else:
output += template1 % ("%s", "%s, "%s", "\n\t"+template2)
但是,我认为 可能 有一种更好的方法来生成这些 XML 模板,而不是将它们分成两个大模板并连接起来,但没有了解有关您的问题的更多详细信息,这将是我的首选解决方案。
我有两个 XML 模板需要在满足某些条件时添加。我连接似乎没有正确处理它们。
template1 = \
'''
<ParentTag>
<orders orderid="%s">
<unitprice>%s</unitprice>
<quantity>%s</quantity>
</orders>
</ParentTag>
'''
template2 = \
'''
<details>
<productid>%s</productid>
<productname>%s</productname>
</details>
我的 objective 是要获取一个 xml 文件,如下所示:
<ParentTag>
<orders orderid="%s">
<unitprice>%s</unitprice>
<quantity>%s</quantity>
</orders>
<details>
<productid>%s</productid>
<productname>%s</productname>
</details>
</ParentTag>
python 代码片段:
output = ""
if (condition is met):
output += output % template1(orderid, unitprice, quantity)
else:
output += <template1 + template2>
如何连接 Python 中的两个模板。
一种(非正统的)方法是这样的:
template1 = \
'''
<ParentTag>
<orders orderid="%s">
<unitprice>%s</unitprice>
<quantity>%s</quantity>
</orders>%s
</ParentTag>
'''
template2 = \
'''
<details>
<productid>%s</productid>
<productname>%s</productname>
</details>
'''
代码:
output = ""
if (condition is met):
output += output % template1(orderid, unitprice, quantity, "")
else:
output += template1 % ("%s", "%s, "%s", "\n\t"+template2)
但是,我认为 可能 有一种更好的方法来生成这些 XML 模板,而不是将它们分成两个大模板并连接起来,但没有了解有关您的问题的更多详细信息,这将是我的首选解决方案。