2

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

<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做到这一点吗?

非常感谢

4

1 回答 1

3

对此没有自动支持。您可以使用 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')
于 2014-04-22T00:42:09.840 回答