[lxml-dev] Bug in XPath evaluation - not a bug :)

Stefan Behnel stefan_ml at behnel.de
Mon Apr 23 08:24:51 CEST 2007


Hi,

Torsten Rehn wrote:
> poc.xml:
> 
> <?xml version="1.0" encoding="utf-8" ?>
> <myrootnode>
>         <myns:mynode xmlns:myns="http.//www.example.com/myns">
>                 <myns:mysubnode>some text</myns:mysubnode>
>         </myns:mynode>
> </myrootnode>
> 
> poc.py:
> 
> #!/usr/bin/env python
> from lxml import etree
> DocTree = etree.parse("poc.xml")
> QueryResult = DocTree.xpath("//myns:mynode")

You should pass the namespace-prefix mapping to lxml. See the docs on this topic:

http://codespeak.net/lxml/dev/xpathxslt.html#xpath


> The result (with added version info):
> 
> [gentop][scel@/home/scel/workspace/lxmlbug] > ./poc.py 
> lxml.etree:        (1, 2, 1, 0)
> libxml used:       (2, 6, 27)
> libxml compiled:   (2, 6, 27)
> libxslt used:      (1, 1, 17)
> libxslt compiled:  (1, 1, 17)
> Traceback (most recent call last):
>   File "./poc.py", line 9, in ?
>     QueryResult = DocTree.xpath("//myns:mynode")
>   File "etree.pyx", line 1256, in etree._ElementTree.xpath
>   File "xpath.pxi", line 75, in etree._XPathEvaluatorBase.evaluate
>   File "xpath.pxi", line 212, in etree.XPathDocumentEvaluator.__call__
>   File "xpath.pxi", line 105, in etree._XPathEvaluatorBase._handle_result
>   File "xpath.pxi", line 93, in etree._XPathEvaluatorBase._raise_parse_error
> etree.XPathSyntaxError: error in xpath expression

As expected. Undefined prefixes are invalid.

Stefan



More information about the lxml-dev mailing list