PyXB添加模式位置

时间:2014-04-18 07:31:01

标签: pyxb

我想知道如何让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做到这一点吗?

非常感谢

1 个答案:

答案 0 :(得分: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')