[Lxml-checkins] r43424 - in lxml/trunk: doc src/lxml src/lxml/tests

scoder at codespeak.net scoder at codespeak.net
Wed May 16 00:21:29 CEST 2007


Author: scoder
Date: Wed May 16 00:21:28 2007
New Revision: 43424

Modified:
   lxml/trunk/doc/objectify.txt
   lxml/trunk/src/lxml/objectify.pyx
   lxml/trunk/src/lxml/tests/test_objectify.py
Log:
always use xsd namespace prefixes for schema types in objectify's xsi:type

Modified: lxml/trunk/doc/objectify.txt
==============================================================================
--- lxml/trunk/doc/objectify.txt	(original)
+++ lxml/trunk/doc/objectify.txt	Wed May 16 00:21:28 2007
@@ -688,19 +688,19 @@
 
     >>> root = objectify.fromstring('''\
     ...    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-    ...      <d xsi:type="double">5</d>
-    ...      <l xsi:type="long"  >5</l>
-    ...      <s xsi:type="string">5</s>
+    ...      <d xsi:type="xsd:double">5</d>
+    ...      <l xsi:type="xsd:long"  >5</l>
+    ...      <s xsi:type="xsd:string">5</s>
     ...    </root>
     ...    ''')
     >>> print objectify.dump(root)
     root = None [ObjectifiedElement]
         d = 5.0 [FloatElement]
-          * xsi:type = 'double'
+          * xsi:type = 'xsd:double'
         l = 5L [LongElement]
-          * xsi:type = 'long'
+          * xsi:type = 'xsd:long'
         s = '5' [StringElement]
-          * xsi:type = 'string'
+          * xsi:type = 'xsd:string'
 
 Again, there is a utility function ``xsiannotate()`` that recursively 
 generates the "xsi:type" attribute for the elements of a tree::
@@ -719,11 +719,11 @@
     >>> print objectify.dump(root)
     root = None [ObjectifiedElement]
         a = 'test' [StringElement]
-          * xsi:type = 'string'
+          * xsi:type = 'xsd:string'
         b = 5 [IntElement]
-          * xsi:type = 'int'
+          * xsi:type = 'xsd:int'
         c = True [BoolElement]
-          * xsi:type = 'boolean'
+          * xsi:type = 'xsd:boolean'
 
 Note, however, that ``xsiannotate()`` will always use the first XML Schema
 datatype that is defined for any given Python type, see also

Modified: lxml/trunk/src/lxml/objectify.pyx
==============================================================================
--- lxml/trunk/src/lxml/objectify.pyx	(original)
+++ lxml/trunk/src/lxml/objectify.pyx	Wed May 16 00:21:28 2007
@@ -1538,7 +1538,8 @@
             c_node, _PYTYPE_NAMESPACE, _PYTYPE_ATTRIBUTE_NAME)
     else:
         # update or create attribute
-        c_ns = cetree.findOrBuildNodeNs(doc, c_node, _PYTYPE_NAMESPACE)
+        c_ns = cetree.findOrBuildNodeNsPrefix(
+            doc, c_node, _PYTYPE_NAMESPACE, 'py')
         tree.xmlSetNsProp(c_node, c_ns, _PYTYPE_ATTRIBUTE_NAME,
                           _cstr(pytype.name))
     tree.END_FOR_EACH_ELEMENT_FROM(c_node)
@@ -1627,16 +1628,21 @@
         cetree.delAttributeFromNsName(c_node, _XML_SCHEMA_INSTANCE_NS, "type")
     else:
         # update or create attribute
-        c_ns = cetree.findOrBuildNodeNs(doc, c_node, _XML_SCHEMA_NS)
+        c_ns = cetree.findOrBuildNodeNsPrefix(
+            doc, c_node, _XML_SCHEMA_NS, 'xsd')
         if c_ns is not NULL:
-            if c_ns.prefix is not NULL and c_ns.prefix[0] != c'\0':
-                if ':' in typename:
-                    oldprefix, name = typename.split(':', 1)
-                    if cstd.strcmp(_cstr(oldprefix), c_ns.prefix) != 0:
-                        typename = c_ns.prefix + ':' + name
-            elif ':' in typename:
-                _, typename = typename.split(':', 1)
-        c_ns = cetree.findOrBuildNodeNs(doc, c_node, _XML_SCHEMA_INSTANCE_NS)
+            if ':' in typename:
+                prefix, name = typename.split(':', 1)
+                if c_ns.prefix is NULL or c_ns.prefix[0] == c'\0':
+                    typename = name
+                elif cstd.strcmp(_cstr(prefix), c_ns.prefix) != 0:
+                    prefix = c_ns.prefix
+                    typename = prefix + ':' + name
+            elif c_ns.prefix is not NULL or c_ns.prefix[0] != c'\0':
+                prefix = c_ns.prefix
+                typename = prefix + ':' + typename
+        c_ns = cetree.findOrBuildNodeNsPrefix(
+            doc, c_node, _XML_SCHEMA_INSTANCE_NS, 'xsi')
         tree.xmlSetNsProp(c_node, c_ns, "type", _cstr(typename))
     tree.END_FOR_EACH_ELEMENT_FROM(c_node)
 
@@ -1763,12 +1769,12 @@
                 raise TypeError, "XSD types require the XSD namespace"
         elif nsmap is _DEFAULT_NSMAP:
             name = _xsi
-            _xsi = 'xsd' + ':' + _xsi
+            _xsi = 'xsd:' + _xsi
         else:
             name = _xsi
-            for p, ns in nsmap.items():
+            for prefix, ns in nsmap.items():
                 if ns == XML_SCHEMA_NS:
-                    if p is not None and p:
+                    if prefix is not None and prefix:
                         _xsi = prefix + ':' + _xsi
                     break
             else:

Modified: lxml/trunk/src/lxml/tests/test_objectify.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_objectify.py	(original)
+++ lxml/trunk/src/lxml/tests/test_objectify.py	Wed May 16 00:21:28 2007
@@ -601,19 +601,19 @@
 
         child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
                         for c in root.iterchildren() ]
-        self.assertEquals("int",   child_types[0])
-        self.assertEquals("string",   child_types[1])
-        self.assertEquals("float", child_types[2])
-        self.assertEquals("string",   child_types[3])
-        self.assertEquals("boolean",  child_types[4])
-        self.assertEquals(None, child_types[5])
-        self.assertEquals(None,    child_types[6])
-        self.assertEquals("int", child_types[7])
-        self.assertEquals("int", child_types[8])
-        self.assertEquals("int", child_types[9])
-        self.assertEquals("string", child_types[10])
-        self.assertEquals("float", child_types[11])
-        self.assertEquals("integer", child_types[12])
+        self.assertEquals("xsd:int",     child_types[0])
+        self.assertEquals("xsd:string",  child_types[1])
+        self.assertEquals("xsd:float",   child_types[2])
+        self.assertEquals("xsd:string",  child_types[3])
+        self.assertEquals("xsd:boolean", child_types[4])
+        self.assertEquals(None,          child_types[5])
+        self.assertEquals(None,          child_types[6])
+        self.assertEquals("xsd:int",     child_types[7])
+        self.assertEquals("xsd:int",     child_types[8])
+        self.assertEquals("xsd:int",     child_types[9])
+        self.assertEquals("xsd:string",  child_types[10])
+        self.assertEquals("xsd:float",   child_types[11])
+        self.assertEquals("xsd:integer", child_types[12])
 
         self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
 
@@ -641,19 +641,19 @@
 
         child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
                         for c in root.iterchildren() ]
-        self.assertEquals("int",   child_types[0])
-        self.assertEquals("string",   child_types[1])
-        self.assertEquals("float", child_types[2])
-        self.assertEquals("string",   child_types[3])
-        self.assertEquals("boolean",  child_types[4])
-        self.assertEquals(None, child_types[5])
-        self.assertEquals(None,    child_types[6])
-        self.assertEquals("double", child_types[7])
-        self.assertEquals("float", child_types[8])
-        self.assertEquals("string", child_types[9])
-        self.assertEquals("string", child_types[10])
-        self.assertEquals("float", child_types[11])
-        self.assertEquals("integer", child_types[12])
+        self.assertEquals("xsd:int",     child_types[0])
+        self.assertEquals("xsd:string",  child_types[1])
+        self.assertEquals("xsd:float",   child_types[2])
+        self.assertEquals("xsd:string",  child_types[3])
+        self.assertEquals("xsd:boolean", child_types[4])
+        self.assertEquals(None,          child_types[5])
+        self.assertEquals(None,          child_types[6])
+        self.assertEquals("xsd:double",  child_types[7])
+        self.assertEquals("xsd:float",   child_types[8])
+        self.assertEquals("xsd:string",  child_types[9])
+        self.assertEquals("xsd:string",  child_types[10])
+        self.assertEquals("xsd:float",   child_types[11])
+        self.assertEquals("xsd:integer", child_types[12])
 
         self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
 
@@ -710,19 +710,19 @@
 
         child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
                         for c in root.iterchildren() ]
-        self.assertEquals("int",      child_types[ 0])
-        self.assertEquals("string",   child_types[ 1])
-        self.assertEquals("float",    child_types[ 2])
-        self.assertEquals("string",   child_types[ 3])
-        self.assertEquals("boolean",  child_types[ 4])
-        self.assertEquals(None,       child_types[ 5])
-        self.assertEquals(None,       child_types[ 6])
-        self.assertEquals("int",      child_types[ 7])
-        self.assertEquals("int",      child_types[ 8])
-        self.assertEquals("int",      child_types[ 9])
-        self.assertEquals("string",   child_types[10])
-        self.assertEquals("float",    child_types[11])
-        self.assertEquals("integer",  child_types[12])
+        self.assertEquals("xsd:int",      child_types[ 0])
+        self.assertEquals("xsd:string",   child_types[ 1])
+        self.assertEquals("xsd:float",    child_types[ 2])
+        self.assertEquals("xsd:string",   child_types[ 3])
+        self.assertEquals("xsd:boolean",  child_types[ 4])
+        self.assertEquals(None,           child_types[ 5])
+        self.assertEquals(None,           child_types[ 6])
+        self.assertEquals("xsd:int",      child_types[ 7])
+        self.assertEquals("xsd:int",      child_types[ 8])
+        self.assertEquals("xsd:int",      child_types[ 9])
+        self.assertEquals("xsd:string",   child_types[10])
+        self.assertEquals("xsd:float",    child_types[11])
+        self.assertEquals("xsd:integer",  child_types[12])
 
         self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
 


More information about the lxml-checkins mailing list