There are two things you'll need to do.
First, see
http://pyxb.sourceforge.net/userref_pyxbgen.html#sharing-namespace-bindings
When you generate the two bindings independently as you are, you end up with
two modules for the shape namespace, the second generated implicitly because
circle depends on it. What you need to do is:
pyxbgen -u shape.xsd -m shape --archive-to-file shape.wxs
pyxbgen -u circle.xsd -m circle --archive-path .:+
so circle.py references shape.py not _shp.py. Otherwise, the superclass of
circle will be _shp.ShapeType rather than shape.ShapeType, which will break
the subclass relationship when testing isinstance(circle_instance,
shape.ShapeType).
Second, as I noted PyXB maintains a registry of known namespaces which is
initialized when a module is loaded. In your shape example (which I assume
is in a new python session) you never load the circle module, so the
infrastructure doesn't know what to do with the c:circle element in the
example file. (The error message is, I'm afraid, very unhelpful.)
Solve this by doing:
from pyxb.utils import domutils
from shape import CreateFromDOM
import circle
p = CreateFromDOM(domutils.StringToDOM(open("example.xml").read()))
print p
You can see the set of namespaces available in the current session with:
import pyxb.namespace
print "\n".join([ _ns.uri() for _ns in
pyxb.namespace.Namespace.AvailableNamespaces() ])
If you aren't able to pre-load the modules corresponding to all the
namespaces you expect, you'll have to implement some sort of registry that
can scan the DOM for a document and verify that all referenced namespaces
are available before proceeding. What you do when you come across an
unrecognized one will depend on your application.
Hope this helps. Sorry the error message is so poor; if you'll file a bug
report at https://sourceforge.net/apps/trac/pyxb/ I'll take a look at
improving things next time I'm working on PyXB.
Peter
On Tue, Feb 22, 2011 at 7:06 PM, Jon Siddle <[email protected]> wrote:
> Thanks for the reply. I'll try to explain with an example below, and any
> help you can give would be much appreciated.
>
> I generate the bindings like this:
>
> $ pyxbgen -u shape.xsd -m shape
> $ pyxbgen -u circle.xsd -m circle
>
> Now the following code works (using the circle package):
>
> from pyxb.utils import domutils
> from circle import CreateFromDOM
> p = CreateFromDOM(domutils.StringToDOM(open("example.xml").read()))
>
> But this doesn't (using the shape package)
>
> from pyxb.utils import domutils
> from shape import CreateFromDOM
> p = CreateFromDOM(domutils.StringToDOM(open("example.xml").read()))
>
> Now in this case I could just use circle, but what if I add circle,
> pentagon, etc?
> Because these are extensions I can't run pyxbgen on them all at once, and
> because they're in different namespaces I shouldn't have to.
>
> example.xml:
>
> <?xml version="1.0"?>
> <picture xmlns="http://www.example.com/shape"<http://www.example.com/shape>
>
> xmlns:c="http://www.example.com/circle"<http://www.example.com/circle>
> >
> <name>Example Picture</name>
> <shapes>
> <square>
> <size>5</size>
> </square>
> <c:circle>
> <c:radius>10</c:radius>
> </c:circle>
> </shapes>
> </picture>
>
>
> shape.xsd:
>
> <?xml version="1.0"?>
> <xs:schema
> xmlns:xs="http://www.w3.org/2001/XMLSchema"<http://www.w3.org/2001/XMLSchema>
> targetNamespace="http://www.example.com/shape"<http://www.example.com/shape>
> xmlns="http://www.example.com/shape" <http://www.example.com/shape>
> elementFormDefault="qualified">
>
> <xs:element name="picture">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="name" type="xs:string"/>
> <xs:element name="shapes" type="ShapesType"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> <xs:complexType name="ShapesType">
> <xs:sequence>
> <xs:element ref="shape" maxOccurs="unbounded"/>
> </xs:sequence>
> </xs:complexType>
>
> <xs:element name="shape" type="ShapeType"/>
> <xs:complexType name="ShapeType"/>
>
> <xs:element name="square" substitutionGroup="shape">
> <xs:complexType>
> <xs:complexContent>
> <xs:extension base="ShapeType">
> <xs:sequence>
> <xs:element name="size" type="xs:int"/>
> </xs:sequence>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> </xs:element>
>
> </xs:schema>
>
> circle.xsd:
>
> <?xml version="1.0"?>
> <xs:schema
> xmlns:xs="http://www.w3.org/2001/XMLSchema"<http://www.w3.org/2001/XMLSchema>
> xmlns:shp="http://www.example.com/shape" <http://www.example.com/shape>
>
> targetNamespace="http://www.example.com/circle"<http://www.example.com/circle>
> xmlns="http://www.example.com/circle" <http://www.example.com/circle>
> elementFormDefault="qualified">
> <xs:import
> namespace="http://www.example.com/shape"<http://www.example.com/shape>schemaLocation="shape.xsd"/>
>
> <xs:element name="circle" substitutionGroup="shp:shape">
> <xs:complexType>
> <xs:complexContent>
> <xs:extension base="shp:ShapeType">
> <xs:sequence>
> <xs:element name="radius" type="xs:int"/>
> </xs:sequence>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> </xs:element>
> </xs:schema>
>
>
> Thanks again, Sorry for the long post.
>
> Jon
>
>
> On 22/02/11 14:17, Peter Bigot wrote:
>
> It's been a while since I've done anything with PyXB, so I may be rusty.
> But what you're describing is supposed to just work.
>
> PyXB maintains a registry of known namespaces, which is initialized when a
> module is loaded. As a document is processed, the relevant namespace should
> be resolved and its content model used.
>
> Although each module contains helper functions CreateFromDocument and
> CreateFromDOM, these should only provide a default namespace. You should be
> able to process a document that has full namespace information by using any
> of the CreateFromDOM functions. (The document must include the necessary
> xmlns:foo="uri" namespace declarations to map the namespace qualifiers to
> the actual namespaces which are URIs.)
>
> If the document uses an absent namespace, then you have to use the
> CreateFromDOM function from the correct module, or identify the correct
> Namespace instance and provide it. (These functions also provide the option
> of overriding the default namespace, though I see a potential bug in that
> CreateFromDocument ignores it in favor of the module namespace.)
>
> I thought I had some multiple namespace examples in the manual, but I don't
> see them offhand. From the distribution, examples/xsdprimer shows an
> example of a document that uses multiple namespaces, as would anything
> involving the OGC schema.
>
> If you continue to have problems, please provide a pointer to a set of
> example schema and a document, along with the application code that isn't
> doing what you think it should, and I'll take a look.
>
> Peter
>
> On Tue, Feb 22, 2011 at 6:19 AM, Jon Siddle <[email protected]> wrote:
>
>> I'm looking at using pyxb for providing an extension mechanism and could
>> do with some guidance.
>>
>> Say I have a.xsd which provides a namespace containing the head of a
>> substitution group, and b.xsd which provides a separate namespace
>> and contains elements which can be substituted.
>>
>> I can generate module a and module b, and I can successfully parse an
>> xml file using b.CreateFromDOM but not a.CreateFromDOM.
>>
>> My problem is that b.xsd (and later c.xsd) will be introduced long after
>> a.xsd. So once I've added c.xsd (module c), how do
>> I process a file which contains substitutions from both b and c?
>>
>> I don't think this is specific to substitution groups at all. More
>> generally, if I have an XML file containing multiple namespaces; how do
>> I parse
>> it?
>>
>> Jon
>>
>>
>> ------------------------------------------------------------------------------
>> Index, Search & Analyze Logs and other IT data in Real-Time with Splunk
>> Collect, index and harness all the fast moving IT data generated by your
>> applications, servers and devices whether physical, virtual or in the
>> cloud.
>> Deliver compliance at lower cost and gain new business insights.
>> Free Software Download: http://p.sf.net/sfu/splunk-dev2dev
>> _______________________________________________
>> pyxb-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/pyxb-users
>>
>
>
>
>
> ------------------------------------------------------------------------------
> Free Software Download: Index, Search & Analyze Logs and other IT data in
> Real-Time with Splunk. Collect, index and harness all the fast moving IT
> data
> generated by your applications, servers and devices whether physical,
> virtual
> or in the cloud. Deliver compliance at lower cost and gain new business
> insights. http://p.sf.net/sfu/splunk-dev2dev
> _______________________________________________
> pyxb-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/pyxb-users
>
>
------------------------------------------------------------------------------
Free Software Download: Index, Search & Analyze Logs and other IT data in
Real-Time with Splunk. Collect, index and harness all the fast moving IT data
generated by your applications, servers and devices whether physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new business
insights. http://p.sf.net/sfu/splunk-dev2dev
_______________________________________________
pyxb-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyxb-users