def test_html_prefix_nsmap(self):
etree = self.etree
html = etree.HTML('<hha:page-description>aa</hha:page-description>')
el = html.find('.//page-description')
if etree.LIBXML_VERSION < (2, 10, 4):
if etree.LIBXML_VERSION < (2, 9, 11): self.assertEqual({'hha': None}, el.nsmap)
else: self.assertEqual({}, el.nsmap)
else: self.assertIsNone(el)
el = html.find('.//hha:page-description') # <-- breaks self.assertEqual({}, el.nsmap)
But the marked line does not work as I expected:
======================================================================
ERROR: test_html_prefix_nsmap (lxml.tests.test_etree.ETreeOnlyTestCase.test_html_prefix_nsmap)
----------------------------------------------------------------------
Traceback (most recent call last):
File "src/lxml/_elementpath.py", line 85, in lxml._elementpath.xpath_tokenizer
raise KeyError
KeyError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib64/python3.11/unittest/case.py", line 57, in testPartExecutor
yield
File "/usr/lib64/python3.11/unittest/case.py", line 623, in run
self._callTestMethod(testMethod)
File "/usr/lib64/python3.11/unittest/case.py", line 579, in _callTestMethod
if method() is not None:
^^^^^^^^
File "/builddir/build/BUILD/lxml-4.9.2/src/lxml/tests/test_etree.py", line 3080, in test_html_prefix_nsmap
el = html.find('.//hha:page-description') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/lxml/etree.pyx", line 1550, in lxml.etree._Element.find
return _elementpath.find(self, path, namespaces)
File "src/lxml/_elementpath.py", line 323, in lxml._elementpath.find
it = iterfind(elem, path, namespaces)
File "src/lxml/_elementpath.py", line 312, in lxml._elementpath.iterfind
selector = _build_path_iterator(path, namespaces)
File "src/lxml/_elementpath.py", line 295, in lxml._elementpath._build_path_iterator
selector.append(ops[token[0]](_next, token))
File "src/lxml/_elementpath.py", line 120, in lxml._elementpath.prepare_descendant
token = next()
File "src/lxml/_elementpath.py", line 88, in xpath_tokenizer
raise SyntaxError("prefix %r not found in prefix map" % prefix)
SyntaxError: prefix 'hha' not found in prefix map
----------------------------------------------------------------------
Ran 1979 tests in 5.332s
I've tried to change the tests like this:
def test_html_ prefix_ nsmap(self) : '<hha:page- description> aa</hha: page-descriptio n>') './/page- description' ) VERSION < (2, 10, 4): VERSION < (2, 9, 11):
self. assertEqual( {'hha': None}, el.nsmap)
self. assertEqual( {}, el.nsmap)
self. assertIsNone( el) './/hha: page-descriptio n') # <-- breaks
self. assertEqual( {}, el.nsmap)
etree = self.etree
html = etree.HTML(
el = html.find(
if etree.LIBXML_
if etree.LIBXML_
else:
else:
el = html.find(
But the marked line does not work as I expected:
======= ======= ======= ======= ======= ======= ======= ======= ======= ======= prefix_ nsmap (lxml.tests. test_etree. ETreeOnlyTestCa se.test_ html_prefix_ nsmap) ------- ------- ------- ------- ------- ------- ------- ------- ------- _elementpath. py", line 85, in lxml._elementpa th.xpath_ tokenizer
ERROR: test_html_
-------
Traceback (most recent call last):
File "src/lxml/
raise KeyError
KeyError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): python3. 11/unittest/ case.py" , line 57, in testPartExecutor python3. 11/unittest/ case.py" , line 623, in run _callTestMethod (testMethod) python3. 11/unittest/ case.py" , line 579, in _callTestMethod build/BUILD/ lxml-4. 9.2/src/ lxml/tests/ test_etree. py", line 3080, in test_html_ prefix_ nsmap './/hha: page-descriptio n')
^^^^^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^^ etree.pyx" , line 1550, in lxml.etree. _Element. find find(self, path, namespaces) _elementpath. py", line 323, in lxml._elementpa th.find _elementpath. py", line 312, in lxml._elementpa th.iterfind path_iterator( path, namespaces) _elementpath. py", line 295, in lxml._elementpa th._build_ path_iterator append( ops[token[ 0]](_next, token)) _elementpath. py", line 120, in lxml._elementpa th.prepare_ descendant _elementpath. py", line 88, in xpath_tokenizer
File "/usr/lib64/
yield
File "/usr/lib64/
self.
File "/usr/lib64/
if method() is not None:
^^^^^^^^
File "/builddir/
el = html.find(
File "src/lxml/
return _elementpath.
File "src/lxml/
it = iterfind(elem, path, namespaces)
File "src/lxml/
selector = _build_
File "src/lxml/
selector.
File "src/lxml/
token = next()
File "src/lxml/
raise SyntaxError("prefix %r not found in prefix map" % prefix)
SyntaxError: prefix 'hha' not found in prefix map
------- ------- ------- ------- ------- ------- ------- ------- ------- -------
Ran 1979 tests in 5.332s