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

scoder at codespeak.net scoder at codespeak.net
Sat May 5 19:02:35 CEST 2007


Author: scoder
Date: Sat May  5 19:02:33 2007
New Revision: 42704

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/src/lxml/extensions.pxi
Log:
support passing a node-set instead of a string in XPath regexps

Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Sat May  5 19:02:33 2007
@@ -8,6 +8,9 @@
 Features added
 --------------
 
+* The regular expression functions in XPath now support passing a node-set
+  instead of a string
+
 * ``Element.addnext(el)`` and ``Element.addprevious(el)`` methods to support
   adding processing instructions and comments around the root node
 
@@ -17,7 +20,7 @@
   plus new ``xsiannotate()`` and ``deannotate()`` functions
 
 * Support for custom Element class instantiation in lxml.sax: passing a
-  ``makeelement()`` function to the ElementTreeContentHandler will reuse the
+  ``makeelement`` function to the ElementTreeContentHandler will reuse the
   lookup context of that function
 
 * '.' represents empty ObjectPath (identity)

Modified: lxml/trunk/src/lxml/extensions.pxi
==============================================================================
--- lxml/trunk/src/lxml/extensions.pxi	(original)
+++ lxml/trunk/src/lxml/extensions.pxi	Sat May  5 19:02:33 2007
@@ -306,6 +306,18 @@
 ################################################################################
 # EXSLT regexp implementation
 
+cdef int _collect_tree_text(element, l) except -1:
+    # recursively collect all text (XPath 'string-value' of a node) 
+    text = element.text
+    if text is not None:
+        python.PyList_Append(l, text)
+    for child in element:
+        _collect_tree_text(child, l)
+    tail = element.tail
+    if tail is not None:
+        python.PyList_Append(l, tail)
+    return 0
+
 cdef class _ExsltRegExp:
     cdef object _compile_map
     def __init__(self):
@@ -314,6 +326,19 @@
     cdef _make_string(self, value):
         if _isString(value):
             return value
+        elif python.PyList_Check(value):
+            # node set: take recursive text concatenation of first element
+            if python.PyList_GET_SIZE(value) == 0:
+                return ''
+            firstnode = value[0]
+            if _isString(firstnode):
+                return firstnode
+            elif isinstance(firstnode, _Element):
+                l = []
+                _collect_tree_text(firstnode, l)
+                return ''.join(l)
+            else:
+                return str(firstnode)
         else:
             raise TypeError, "Invalid argument type %s" % type(value)
 


More information about the lxml-checkins mailing list