There are two errors in your schema.  First, the targetNamespace attribute
is not in the http://www.w3.org/2001/XMLSchema namespace and should not be
so-qualified.  The effect is that your declared type and element is
{}sthType and {}something.  Your document looks for element {
http://carfax.org.uk/namespace}something but that element doesn't exist so
PyXB complains.

Once you fix that the second error becomes clear: Because the schema
doesn't have a default namespace and you don't have a namespace declaration
for your target namespace, the bindings can't be generated because the
element declaration uses an unqualified name sthType in a context where a
namespace can't be inferred.

This schema should do what you want without changing your program or
document.

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema targetNamespace="http://carfax.org.uk/namespace";
        xmlns:tns="http://carfax.org.uk/namespace";
        xmlns:xsd="http://www.w3.org/2001/XMLSchema";>
  <xsd:complexType name="sthType">
    <xsd:attribute name="foo" type="xsd:string"/>
  </xsd:complexType>
  <xsd:element name="something" type="tns:sthType"/>
</xsd:schema>

Peter

On Sat, May 16, 2015 at 9:40 AM, Hugo Mills <h...@carfax.org.uk> wrote:

>    Hi, I'm having trouble getting namespaced documents to work.
>
>    I have this (test) schema:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <xsd:schema xsd:targetNamespace="http://carfax.org.uk/namespace";
>                         xmlns:xsd="http://www.w3.org/2001/XMLSchema";>
>   <xsd:complexType name="sthType">
>   <xsd:attribute name="foo" type="xsd:string"/>
>   </xsd:complexType>
> <xsd:element name="something" type="sthType"/>
> </xsd:schema>
>
> and this XML document, which should, I believe, conform to the schema:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <something xmlns="http://carfax.org.uk/namespace"; foo="bar">
> </something>
>
> I've generated a module (PyXB 1.2.4):
>
> $ pyxbgen -u schema.xsd -m sth
> Python for AbsentNamespace0 requires 1 modules
>
> and I'm now trying to open the document:
>
> #!/usr/bin/python3
> import sth
> doc = sth.CreateFromDocument(open("doc.xml", "r").read())
> print(doc)
>
> This gives me:
>
> $ ./test
> Traceback (most recent call last):
>   File
> "/usr/local/lib/python3.4/dist-packages/pyxb/namespace/__init__.py", line
> 307, in categoryMap
>     return self.__categoryMap[category]
> KeyError: 'elementBinding'
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>   File "./test", line 5, in <module>
>     doc = sth.CreateFromDocument(open("doc.xml", "r").read())
>   File "/home/hrm/rostercare/temp/sth.py", line 62, in CreateFromDocument
>     saxer.parse(io.BytesIO(xmld))
>   File "/usr/lib/python3.4/xml/sax/expatreader.py", line 107, in parse
>     xmlreader.IncrementalParser.parse(self, source)
>   File "/usr/lib/python3.4/xml/sax/xmlreader.py", line 123, in parse
>     self.feed(buffer)
>   File "/usr/lib/python3.4/xml/sax/expatreader.py", line 207, in feed
>     self._parser.Parse(data, isFinal)
>   File "/usr/lib/python3.4/xml/sax/expatreader.py", line 341, in
> start_element_ns
>     AttributesNSImpl(newattrs, qnames))
>   File "/usr/local/lib/python3.4/dist-packages/pyxb/binding/saxer.py",
> line 324, in startElementNS
>     element_binding = name_en.elementBinding()
>   File
> "/usr/local/lib/python3.4/dist-packages/pyxb/namespace/__init__.py", line
> 104, in __getattr__
>     category_value = ns.categoryMap(name).get(self.localName())
>   File
> "/usr/local/lib/python3.4/dist-packages/pyxb/namespace/__init__.py", line
> 309, in categoryMap
>     raise pyxb.NamespaceError(self, '%s has no category %s' % (self,
> category))
> pyxb.exceptions_.NamespaceError: http://carfax.org.uk/namespace has no
> category elementBinding
>
>    Now, I'm guessing that the problem is indicated with the "Python
> for AbsentNamespace0 requires 1 module" message from pyxbgen, but I'm
> at a loss to see what the problem is.
>
>    The only concern I have, which is more likely to be a basic XML
> question than a PyXB-specific one, is that only the elements in the
> schema are namespaced, not the attributes, with the exception of the
> xsd:targetNamespace attribute. I can't get pyxbgen to run with xsd:
> namespaces on the attributes of the schema, nor with the xsd:
> namespace missing from the xsd:targetNamespace attribute. This
> confuses me somewhat, in my primitive understanding of XML Schema.
>
>    What am I doing wrong in all of this?
>
>    Thanks,
>    Hugo.
>
> --
> Hugo Mills             | The problem with programmers and the law is not
> that
> hugo@... carfax.org.uk | they treat laws as code, but that they don't
> http://carfax.org.uk/  | understand the VM it runs on.
> PGP: E2AB1DE4          |
>
>
> ------------------------------------------------------------------------------
> One dashboard for servers and applications across Physical-Virtual-Cloud
> Widest out-of-the-box monitoring support with 50+ applications
> Performance metrics, stats and reports that give you Actionable Insights
> Deep dive visibility with transaction tracing using APM Insight.
> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
> _______________________________________________
> pyxb-users mailing list
> pyxb-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/pyxb-users
>
>
------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
pyxb-users mailing list
pyxb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyxb-users

Reply via email to