如何使用 python 更改 xml 中属性的命名空间?

问题描述 投票:0回答:0

就我而言,我有以下代码:

from lxml import etree as et
from lxml.builder import ElementMaker

WSS_SCHEMA = "http://schemas.xmlsoap.org/ws/2002/12/secext"
ACTOR_SCHEMA = "http://xpto"
VERSION_SCHEMA = "2"

NSMAP = {
    'wss': WSS_SCHEMA,
    'S': ACTOR_SCHEMA,
    'at': VERSION_SCHEMA,
}

WSS = ElementMaker(namespace=WSS_SCHEMA, nsmap=NSMAP)

security = WSS.Security()

security.set("{%s}actor" % ACTOR_SCHEMA, ACTOR_SCHEMA)
security.set("{%s}Version" % VERSION_SCHEMA, VERSION_SCHEMA)

print(et.tostring(security, encoding="unicode"))

我在输出中得到这个:

我想要的输出是这样的:

有没有人对我如何删除这两个属性有任何建议,或者我如何只添加所需的属性及其各自的命名空间?

python xml web-services elementtree xml-namespaces
© www.soinside.com 2019 - 2024. All rights reserved.