2014-04-18 26 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')