0
從這個XSD文件:如何用PyXB或JaXB去除'ns1'前綴?
<?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
不幸的是我得到了一個XML不想要的ns1
前綴:
<?xml version="1.0" ?>
<ns1:Foo xmlns:ns1="http://foo.org/FooIsNotBar">Hello World</ns1:Foo>
我想擺脫這些ns1:
前綴。怎麼樣?
編輯
這question有關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, '')