Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e9927e18
Commit
e9927e18
authored
Apr 14, 2019
by
Stefan Behnel
Committed by
GitHub
Apr 14, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-30485: support a default prefix mapping in ElementPath by passing None as prefix (#1823)
parent
ffca16e2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
12 deletions
+39
-12
Doc/library/xml.etree.elementtree.rst
Doc/library/xml.etree.elementtree.rst
+6
-3
Lib/test/test_xml_etree.py
Lib/test/test_xml_etree.py
+6
-0
Lib/xml/etree/ElementPath.py
Lib/xml/etree/ElementPath.py
+24
-9
Misc/NEWS.d/next/Library/2019-04-13-23-42-33.bpo-30485.JHhjJS.rst
...S.d/next/Library/2019-04-13-23-42-33.bpo-30485.JHhjJS.rst
+3
-0
No files found.
Doc/library/xml.etree.elementtree.rst
View file @
e9927e18
...
...
@@ -764,7 +764,8 @@ Element Objects
Finds the first subelement matching *match*. *match* may be a tag name
or a :ref:`path
<elementtree-xpath>
`. Returns an element instance
or ``None``. *namespaces* is an optional mapping from namespace prefix
to full name.
to full name. Pass ``None`` as prefix to move all unprefixed tag names
in the expression into the given namespace.
.. method:: findall(match, namespaces=None)
...
...
@@ -772,7 +773,8 @@ Element Objects
Finds all matching subelements, by tag name or
:ref:`path
<elementtree-xpath>
`. Returns a list containing all matching
elements in document order. *namespaces* is an optional mapping from
namespace prefix to full name.
namespace prefix to full name. Pass ``None`` as prefix to move all
unprefixed tag names in the expression into the given namespace.
.. method:: findtext(match, default=None, namespaces=None)
...
...
@@ -782,7 +784,8 @@ Element Objects
of the first matching element, or *default* if no element was found.
Note that if the matching element has no text content an empty string
is returned. *namespaces* is an optional mapping from namespace prefix
to full name.
to full name. Pass ``None`` as prefix to move all unprefixed tag names
in the expression into the given namespace.
.. method:: getchildren()
...
...
Lib/test/test_xml_etree.py
View file @
e9927e18
...
...
@@ -2463,6 +2463,12 @@ class ElementFindTest(unittest.TestCase):
nsmap
=
{
'xx'
:
'Y'
}
self
.
assertEqual
(
len
(
root
.
findall
(
".//xx:b"
,
namespaces
=
nsmap
)),
1
)
self
.
assertEqual
(
len
(
root
.
findall
(
".//b"
,
namespaces
=
nsmap
)),
2
)
nsmap
=
{
'xx'
:
'X'
,
None
:
'Y'
}
self
.
assertEqual
(
len
(
root
.
findall
(
".//xx:b"
,
namespaces
=
nsmap
)),
2
)
self
.
assertEqual
(
len
(
root
.
findall
(
".//b"
,
namespaces
=
nsmap
)),
1
)
nsmap
=
{
'xx'
:
'X'
,
''
:
'Y'
}
with
self
.
assertRaisesRegex
(
ValueError
,
'namespace prefix'
):
root
.
findall
(
".//xx:b"
,
namespaces
=
nsmap
)
def
test_bad_find
(
self
):
e
=
ET
.
XML
(
SAMPLE_XML
)
...
...
Lib/xml/etree/ElementPath.py
View file @
e9927e18
...
...
@@ -71,16 +71,22 @@ xpath_tokenizer_re = re.compile(
)
def xpath_tokenizer(pattern, namespaces=None):
default_namespace = namespaces.get(None) if namespaces else None
for token in xpath_tokenizer_re.findall(pattern):
tag = token[1]
if tag and tag[0] != "
{
"
and "
:
" in tag
:
try
:
if tag and tag[0] != "
{
":
if "
:
" in tag
:
prefix, uri = tag.split("
:
", 1)
if not namespaces:
raise KeyError
yield token[0], "
{
%
s
}
%
s
" % (namespaces[prefix], uri)
except KeyError:
raise SyntaxError("
prefix
%
r
not
found
in
prefix
map
" % prefix) from None
try:
if not namespaces:
raise KeyError
yield token[0], "
{
%
s
}
%
s
" % (namespaces[prefix], uri)
except KeyError:
raise SyntaxError("
prefix
%
r
not
found
in
prefix
map
" % prefix) from None
elif default_namespace:
yield token[0], "
{
%
s
}
%
s
" % (default_namespace, tag)
else:
yield token
else:
yield token
...
...
@@ -264,10 +270,19 @@ class _SelectorContext:
def iterfind(elem, path, namespaces=None):
# compile selector pattern
cache_key = (path, None if namespaces is None
else tuple(sorted(namespaces.items())))
if path[-1:] == "/":
path = path + "*" # implicit all (FIXME: keep this?)
cache_key = (path,)
if namespaces:
if '' in namespaces:
raise ValueError("empty namespace prefix must be passed as None, not the empty string")
if None in namespaces:
cache_key += (namespaces[None],) + tuple(sorted(
item for item in namespaces.items() if item[0] is not None))
else:
cache_key += tuple(sorted(namespaces.items()))
try:
selector = _cache[cache_key]
except KeyError:
...
...
Misc/NEWS.d/next/Library/2019-04-13-23-42-33.bpo-30485.JHhjJS.rst
0 → 100644
View file @
e9927e18
Path expressions in xml.etree.ElementTree can now avoid explicit namespace
prefixes for tags (or the "{namespace}tag" notation) by passing a default
namespace with a 'None' prefix.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment