【发布时间】:2016-11-01 14:12:19
【问题描述】:
来自这个 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 的 question 给了我一些提示,但是我还没有找到解决问题的方法。
我发现我可以使用test.Namespace.setPrefix('foo') 设置我的前缀。不幸的是,我无法隐藏前缀。
一个肮脏的解决方案是这样做:
import test
rmp = 'REMOVE_ME_PLEASE'
test.Namesapce.setPrefix(rmp)
print test.Foo('Hello World').toxml().replace(rmp + ':', '').replace(':' + rmp, '')
【问题讨论】:
标签: python xml jaxb xml-namespaces pyxb