Re: [lxml-dev] [objectify] schema type registry: QNames for xsi:type?
Hi Holger, jholg@gmx.de wrote:
Is it easily possible to use QNames in the xsi-type lookup system? I believe this would be the right thing to do, as lxml should be consistent. I gave it a preliminary implementation on the trunk, could you check if
jholg@gmx.de wrote: this works for you?
With this small fix:
*** src/lxml/objectify.pyx.ORIG Tue May 15 16:24:31 2007 --- src/lxml/objectify.pyx Tue May 15 16:23:14 2007 *************** *** 1768,1774 **** name = _xsi for p, ns in nsmap.items(): if ns == XML_SCHEMA_NS: ! _xsi = prefix + ':' + _xsi break else: raise TypeError, "XSD types require the XSD namespace" --- 1768,1775 ---- name = _xsi for p, ns in nsmap.items(): if ns == XML_SCHEMA_NS: ! if p: ! _xsi = p + ':' + _xsi break else: raise TypeError, "XSD types require the XSD namespace"
it mostly works for me.
Sure, thanks.
However, I detected that the nice nsmap-unification currently does not handle attributes:
msg = etree.fromstring("""<msg xmlns:FOOBAR="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>""") s = DataElement("234837", _xsi="string", nsmap={'myXSD': 'http://www.w3.org/2001/XMLSchema'})
print etree.tostring(s, pretty_print=1) <value xmlns:myXSD="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://codespeak.net/lxml/objectify/pytype" xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" ns0:pytype="str" ns1:type="myXSD:string">234837</value> print etree.tostring(msg, pretty_print=1) <msg xmlns:FOOBAR="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> msg.s = s print etree.tostring(msg, pretty_print=1) <msg xmlns:FOOBAR="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s xmlns:ns0="http://codespeak.net/lxml/objectify/pytype" ns0:pytype="str" xsi:type="myXSD:string">234837</s> </msg>
Now that nsmap-unification has taken place, the myXSD-prefix has not been changed to FOOBAR. I fear this is not a nice one to fix as all the attribute-value prefixes had to be checked. So basically I think this is not a problem of objectify.
Definitely not. While we could potentially look for attributes that we created ourselves, I don't think it is worth having lxml handle this. Users should take care when they use their own prefixes. Maybe worth a remark in the docs...
Not really related to this, I detected what I think is a slight inconsistency regarding nsmap when using None vs '' as nsmap-keys (=prefixes):
s = DataElement("234837", _xsi="string", nsmap={'': 'http://www.w3.org/2001/XMLSchema'}) print etree.tostring(s, pretty_print=1) <value xmlns:="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://codespeak.net/lxml/objectify/pytype" xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" ns0:pytype="str" ns1:type="string">234837</value> s = DataElement("234837", _xsi="string", nsmap={None: 'http://www.w3.org/2001/XMLSchema'}) print etree.tostring(s, pretty_print=1) <value xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://codespeak.net/lxml/objectify/pytype" xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" ns0:pytype="str" ns1:type="string">234837</value>
Though I'm not sure if this really a bug, a mere inconvenience, or even valid (is xmlns:=... allowed?)
Maybe this is rather a user error, but I don't see why lxml should not just convert this to None internally. So, two fixes applied, please check again. :) Stefan
participants (1)
-
Stefan Behnel