2014-04-18 30 views
1

我想知道如何让pyxb将模式位置添加到生成的xml中,例如。PyXB添加架构位置

<ns1:myDocument xmlns:ns1="http://www.mydomain.com/path" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.mydomain.com/path myschema.xsd"> 

在JAXB我会做到这一点与

jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, 
     "http://www.mydomain.com/path myschema.xsd"); 

任何想法如何做到这一点与pyxb?

非常感谢

回答

2

对此没有自动支持。您可以使用toDOM()方法,然后使用DOM命令将该属性添加到文档中。请注意,除非您已在文档中使用xsi命名空间,否则您还必须添加它。

import xml.dom.minidom 
from pyxb.namespace import XMLSchema_instance as xsi 
from pyxb.namespace import XMLNamespaces as xmlns 
v = instance.toDOM() 
v.documentElement.setAttributeNS(xsi.uri(), 'xsi:schemaLocation', 'urn:someurn') 
v.documentElement.setAttributeNS(xmlns.uri(), 'xmlns:xsi', xsi.uri()) 
print v.toxml('utf-8')