从这个 xsd 文件:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://foo.org/FooIsNotBar"
elementFormDefault="qualified">
<xs:element name="Foo" type="xs:string"/>
</xs:schema>
我想使用 PyXB 来获取这个 XML:
<?xml version="1.0" ?>
<Foo xmlns="http://foo.org/FooIsNotBar">hello</Foo>
所以我这样做了:
pyxbgen -m test -u test.xsd # Where test.xsd is the above xsd file
echo -e "import test\\nprint test.Foo('Hello World').toxml()" | python
不幸的是,我得到了一个带有不需要的ns1
前缀的 XML:
<?xml version="1.0" ?>
<ns1:Foo xmlns:ns1="http://foo.org/FooIsNotBar">Hello World</ns1:Foo>
我想摆脱这些ns1:
前缀。如何?
编辑
这个关于jaxb 的问题给了我一些提示,但是我还没有找到解决我的问题的方法。
我发现我可以使用test.Namespace.setPrefix('foo')
. 不幸的是,我无法隐藏前缀。
一个肮脏的解决方案是这样做:
import test
rmp = 'REMOVE_ME_PLEASE'
test.Namesapce.setPrefix(rmp)
print test.Foo('Hello World').toxml().replace(rmp + ':', '').replace(':' + rmp, '')