Search code examples
pyxb

PyXB add schema location


I wonder how I could get pyxb to add the schema location to generated xml, eg.

<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">

In JAXB I'd achieve that with

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

Any idea how to do that with pyxb?

Many thanks


Solution

  • There is no automatic support for this. You can use the toDOM() method and then DOM commands to add the attribute to the document. Note that, unless you already use the xsi namespace in your document you'll have to add it too.

    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')