searching for XML elements with position predicates does not work at all when namespaces are involved.
未關閉
還沒有人認領這個 Issue。
extension-modules
topic-XML
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先使用 xml.etree.ElementTree 重現 namespace 範例,將位置述詞和 last() 述詞與無 namespace XML 所展示的行為進行比較。追蹤 find() 使用的 ElementTree 路徑搜尋實作;當使用預設 namespace 時兩個述詞都返回預期元素,即表示完成。已存在一個關聯的 PR:gh-132822。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100