Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Doc/library/xml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ The XML handling submodules are:
* :mod:`xml.sax`: SAX2 base classes and convenience functions
* :mod:`xml.parsers.expat`: the Expat parser binding

This module also defines utility functions.

.. function:: is_valid_name(name)

Return ``True`` if the string is a valid element or attribute name,
``False`` otherwise.

Almost all characters are permitted in names, except control characters and
those which either are or reasonably could be used as delimiters.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a link to the specs if any?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link found in the implementation: https://www.w3.org/TR/xml/#NT-Name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to add it in the online docs as well. It'll be more accessible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to add several more functions. Some functions will need several links, and some links will be repeated for different functions. If we want to add links, it is better to add root links to the XML specifications at the top of the module documentation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also ok. As long as the online docs contain some links to some specifications, it's ok.

Characters like ":", "-", ".", "_", and "·" are permitted, but "<", "/",
"!", "?", and "=" are forbidden.
The name cannot start with a digit or a character like "-", ".", and "·".

..versionadded:: next


.. _xml-security:
.. _xml-vulnerabilities:
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,14 @@ wave
(Contributed by Lionel Koenig and Michiel W. Beijen in :gh:`60729`.)


xml
---

* Add the :func:`xml.is_valid_name` function, which allows to check
whether a string can be used as an element or attribute name in XML.
(Contributed by Serhiy Storchaka in :gh:`139489`.)


xml.parsers.expat
-----------------

Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import xml
import unittest


class TestUtils(unittest.TestCase):

def test_is_valid_name(self):
is_valid_name = xml.is_valid_name
self.assertFalse(is_valid_name(''))
self.assertTrue(is_valid_name('name'))
self.assertTrue(is_valid_name('NAME'))
self.assertTrue(is_valid_name('name0:-._·'))
self.assertTrue(is_valid_name('_'))
self.assertTrue(is_valid_name(':'))
self.assertTrue(is_valid_name('Ñàḿĕ'))
self.assertTrue(is_valid_name('\U000EFFFF'))
self.assertFalse(is_valid_name('0'))
self.assertFalse(is_valid_name('-'))
self.assertFalse(is_valid_name('.'))
self.assertFalse(is_valid_name('·'))
self.assertFalse(is_valid_name('na me'))
for c in '<>/!?=\x00\x01\x7f\ud800\udfff\ufffe\uffff\U000F0000':
self.assertFalse(is_valid_name('name' + c))


if __name__ == '__main__':
unittest.main()
3 changes: 2 additions & 1 deletion Lib/xml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@

"""

from .utils import *

__all__ = ["dom", "parsers", "sax", "etree"]
__all__ = ["dom", "parsers", "sax", "etree", "is_valid_name"]
25 changes: 25 additions & 0 deletions Lib/xml/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
lazy import re as _re


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding an __all__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed. Currently, the re module is exposed as a xml attribute:

$ ./python
>>> import xml
>>> xml.re
<module 're' from '/home/vstinner/python/main/Lib/re/__init__.py'>

def is_valid_name(name):
"""Test whether a string is a valid element or attribute name."""
# https://www.w3.org/TR/xml/#NT-Name
return _re.fullmatch(
# NameStartChar
'['
':A-Z_a-z'
'\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF'
'\u200C\u200D'
'\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF'
'\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF'
']'
# NameChar
'['
r'\-.0-9:A-Z_a-z'
'\xB7'
'\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF'
'\u200C\u200D\u203F\u2040'
'\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF'
'\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF'
']*+',
name) is not None
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add the :func:`xml.is_valid_name` function, which allows to check
whether a string can be used as an element or attribute name in XML.
Loading