Search code examples
xmlpyxb

How to make Pyxb not to generate xsi:type=string?


We have a library using PyXB for generating XML files to submit to our upstream payment processor, the simplified code looks like this

import pyxb.binding.datatypes as pyxbd

from . import schema

record = schema.Record(
    Arg1='xxx',
    Arg2='xxx',
)
record.Foo = pyxbd.string('bar')
print record.toDOM()

And the generated XML document will look like this

<?xml version="1.0" encoding="utf-8"?>
<Record Arg1="xxx" Arg2="xxx" 
  xmlns:ns1="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
    <Foo xsi:type="ns1:string">Bar</Foo>
<Record>

Don't know why, but our upstream processor doesn't like the xsi:type="ns1:string" attribute for any elements, so it rejects our requests. My question here is why is that pyxb schema generates this xsi:type thing? and I can I stop it from generating it?


Solution

  • Without seeing the schema a definitive answer can't be given. xsi:type can be added by the infrastructure that generates the XML from an instance in order to work around improper schema embedded in WSDL documents, but this is not the default. A related case is when an element has type xs:any or has an abstract type but the value isn't compatible with it: in those cases PyXB adds the type to ensure the receiver can interpret the element content.

    In short, the highest probability is that the schema is incomplete, but it's possible PyXB is being obtuse. If you can reproduce the problem in a self-contained schema, please file an issue on github.

    Alternatively, if you can't modify the schema to make the element type deterministic you could generate a DOM structure via dom = instance.toDOM() then use xml.dom operations to remove the offending attribute before converting it to XML with dom.documentElement.toxml('utf-8').