[Lxml-checkins] r48316 - in lxml/trunk: . doc src/lxml

scoder at codespeak.net scoder at codespeak.net
Mon Nov 5 15:27:50 CET 2007


Author: scoder
Date: Mon Nov  5 15:27:46 2007
New Revision: 48316

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/doc/objectify.txt
   lxml/trunk/doc/tutorial.txt
   lxml/trunk/src/lxml/lxml.etree.pyx
Log:
use default prefixes for common namespaces (following ET 1.3)

Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Mon Nov  5 15:27:46 2007
@@ -8,6 +8,8 @@
 Features added
 --------------
 
+* Use default prefixes for some common XML namespaces
+
 * ``lxml.html.clean.Cleaner`` now allows for a ``host_whitelist``, and
   two overridable methods: ``allow_embedded_url(el, url)`` and the
   more general ``allow_element(el)``.

Modified: lxml/trunk/doc/objectify.txt
==============================================================================
--- lxml/trunk/doc/objectify.txt	(original)
+++ lxml/trunk/doc/objectify.txt	Mon Nov  5 15:27:46 2007
@@ -956,12 +956,15 @@
     >>> for prefix, namespace in el.nsmap.items():
     ...     print prefix, '-', namespace
     ns0 - http://codespeak.net/lxml/objectify/pytype
-    ns1 - http://www.w3.org/2001/XMLSchema-instance
     foo - http://www.w3.org/2001/XMLSchema
+    xsi - http://www.w3.org/2001/XMLSchema-instance
 
     >>> print el.get("{http://www.w3.org/2001/XMLSchema-instance}type")
     foo:string
 
+Note how lxml chose a default prefix for the XML Schema Instance
+namespace.  We can override it as in the following example::
+
     >>> el = objectify.DataElement('5', _xsi='foo:string',
     ...          nsmap={'foo': 'http://www.w3.org/2001/XMLSchema',
     ...                 'myxsi': 'http://www.w3.org/2001/XMLSchema-instance'})

Modified: lxml/trunk/doc/tutorial.txt
==============================================================================
--- lxml/trunk/doc/tutorial.txt	(original)
+++ lxml/trunk/doc/tutorial.txt	Mon Nov  5 15:27:46 2007
@@ -658,9 +658,9 @@
     >>> body.text = "Hello World"
 
     >>> print etree.tostring(xhtml, pretty_print=True)
-    <ns0:html xmlns:ns0="http://www.w3.org/1999/xhtml">
-      <ns0:body>Hello World</ns0:body>
-    </ns0:html>
+    <html:html xmlns:html="http://www.w3.org/1999/xhtml">
+      <html:body>Hello World</html:body>
+    </html:html>
 
 .. _`namespace prefixes`: http://www.w3.org/TR/xml-names/#ns-qualnames
 

Modified: lxml/trunk/src/lxml/lxml.etree.pyx
==============================================================================
--- lxml/trunk/src/lxml/lxml.etree.pyx	(original)
+++ lxml/trunk/src/lxml/lxml.etree.pyx	Mon Nov  5 15:27:46 2007
@@ -68,6 +68,17 @@
 cdef char* _C_FILENAME_ENCODING
 _C_FILENAME_ENCODING = _cstr(_FILENAME_ENCODING)
 
+# set up some default namespace prefixes
+_DEFAULT_NAMESPACE_PREFIXES = {
+    "http://www.w3.org/1999/xhtml": "html",
+    "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
+    "http://schemas.xmlsoap.org/wsdl/": "wsdl",
+    # xml schema
+    "http://www.w3.org/2001/XMLSchema": "xs",
+    "http://www.w3.org/2001/XMLSchema-instance": "xsi",
+    # dublic core
+    "http://purl.org/dc/elements/1.1/": "dc",
+}
 
 # Error superclass for ElementTree compatibility
 class Error(Exception):
@@ -323,6 +334,7 @@
         """
         cdef xmlNs* c_ns
         cdef xmlNs* c_doc_ns
+        cdef python.PyObject* dict_result
         if c_node.type != tree.XML_ELEMENT_NODE:
             assert c_node.type == tree.XML_ELEMENT_NODE, \
                 "invalid node type %d, expected %d" % (
@@ -332,6 +344,12 @@
         if c_ns is not NULL:
             return c_ns
 
+        if c_prefix is NULL:
+            dict_result = python.PyDict_GetItemString(
+                _DEFAULT_NAMESPACE_PREFIXES, c_href)
+            if dict_result is not NULL:
+                c_prefix = _cstr(<object>dict_result)
+
         if c_prefix is NULL or \
                tree.xmlSearchNs(self._c_doc, c_node, c_prefix) is not NULL:
             # try to simulate ElementTree's namespace prefix creation


More information about the lxml-checkins mailing list