searching for XML elements with position predicates does not work at all when namespaces are involved.
Aperta
Nessuno ha ancora preso questa issue.
extension-modules
topic-XML
type-bug
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
Failure with namespaces:
Python 3.12.7 | packaged by conda-forge | (main, Oct 4 2024, 16:05:46) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> ET.XML('<a xmlns="http://www.example.com/default-schema"><b/><b/></a>').find("./b[1]", {"": "http://www.example.com/default-schema"})
>>> ET.XML('<a xmlns="http://www.example.com/default-schema"><b/><b/></a>').find("./b[last()]", {"": "http://www.example.com/default-schema"})
Traceback (most recent call last):
File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 370, in iterfind
selector = _cache[cache_key]
~~~~~~^^^^^^^^^^^
KeyError: ('./b[last()]', ('', 'http://www.example.com/default-schema'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 405, in find
return next(iterfind(elem, path, namespaces), None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 384, in iterfind
selector.append(ops[token[0]](next, token))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 315, in prepare_predicate
raise SyntaxError("unsupported function")
SyntaxError: unsupported function
>>> ET.XML('<a xmlns="http://www.example.com/default-schema"><b/><b/></a>').find("./b", {"": "http://www.example.com/default-schema"})
<Element '{http://www.example.com/default-schema}b' at 0x750110149b20>
>>>
- searching by integer position yields
None - searching with the predicate
[last()]fails with "unsupported function"
This works fine when no namespaces are involved:
\>>> ET.XML('<a><b/><b/></a>').find("./b[1]")
<Element 'b' at 0x7501101498a0>
>>> ET.XML('<a><b/><b/></a>').find("./b[last()]")
<Element 'b' at 0x7501101499e0>
>>>
With namespaces I would expect similar results.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
- gh-132822
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo gli esempi di namespace con xml.etree.ElementTree e confronta i predicati posizionali e last() con il comportamento mostrato per l’XML senza namespace. Traccia l’implementazione della ricerca dei percorsi di ElementTree utilizzata da find(); il lavoro è completato quando entrambi i predicati restituiscono gli elementi attesi con un namespace predefinito. Esiste già un PR collegato, gh-132822.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100