Traceback (most recent call last):
File "/Users/user/Desktop/function-namespace.py", line 7, in <module>
print _xml.xpath('count()')
File "src/lxml/etree.pyx", line 1575, in lxml.etree._Element.xpath
File "src/lxml/xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__
File "src/lxml/xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid number of arguments
Seems it doesn't recognise my 'count(arg)' function and use standard 'count(...)' instead.
It works fine with custom functions that are not conflicting with standard functions, but how can I specify a custom function with one of the standard functions name?
If I specify a namespace with a prefix, a custom function works fine:
from lxml import etree amespace( 'http:// example. com') ns['count' ] = _dummy_count 'structure' ) 'a:count( )')
_function_ns = etree.FunctionN
_function_ns.prefix = 'a'
def _dummy_count(arg):
return 10
_function_
_xml = etree.Element(
print _xml.xpath(
Output is '10.0'.
But if I add the function to the default namespace, I've got an error:
from lxml import etree amespace( None) ns['count' ] = _dummy_count 'structure' ) 'count( )')
_function_ns = etree.FunctionN
def _dummy_count(arg):
return 10
_function_
_xml = etree.Element(
print _xml.xpath(
Output:
Traceback (most recent call last): user/Desktop/ function- namespace. py", line 7, in <module> 'count( )') etree.pyx" , line 1575, in lxml.etree. _Element. xpath xpath.pxi" , line 307, in lxml.etree. XPathElementEva luator. __call_ _ xpath.pxi" , line 227, in lxml.etree. _XPathEvaluator Base._handle_ result XPathEvalError: Invalid number of arguments
File "/Users/
print _xml.xpath(
File "src/lxml/
File "src/lxml/
File "src/lxml/
lxml.etree.
Seems it doesn't recognise my 'count(arg)' function and use standard 'count(...)' instead.
It works fine with custom functions that are not conflicting with standard functions, but how can I specify a custom function with one of the standard functions name?