Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(161121)

Side by Side Diff: Lib/xml/etree/ElementTree.py

Issue 18304: ElementTree -- provide a way to ignore namespace in tags and seaches
Patch Set: Created 3 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_xml_etree.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Lightweight XML support for Python. 1 """Lightweight XML support for Python.
2 2
3 XML is an inherently hierarchical data format, and the most natural way to 3 XML is an inherently hierarchical data format, and the most natural way to
4 represent it is with a tree. This module has two classes for this purpose: 4 represent it is with a tree. This module has two classes for this purpose:
5 5
6 1. ElementTree represents the whole XML document as a tree and 6 1. ElementTree represents the whole XML document as a tree and
7 7
8 2. Element represents a single node in this tree. 8 2. Element represents a single node in this tree.
9 9
10 Interactions with the whole document (reading and writing to/from files) are 10 Interactions with the whole document (reading and writing to/from files) are
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 return self.text >= other.text 528 return self.text >= other.text
529 return self.text >= other 529 return self.text >= other
530 def __gt__(self, other): 530 def __gt__(self, other):
531 if isinstance(other, QName): 531 if isinstance(other, QName):
532 return self.text > other.text 532 return self.text > other.text
533 return self.text > other 533 return self.text > other
534 def __eq__(self, other): 534 def __eq__(self, other):
535 if isinstance(other, QName): 535 if isinstance(other, QName):
536 return self.text == other.text 536 return self.text == other.text
537 return self.text == other 537 return self.text == other
538
539
540 def strip_namespaces(tree):
541 """Remove all namespaces from tags and attributes in place.
542
543 Leaves only the local names in the subtree.
544 """
545 for el in tree.iter():
546 tag = el.tag
547 if tag and isinstance(tag, str) and tag[0] == '{':
548 el.tag = tag.partition('}')[2]
549 attrib = el.attrib
550 if attrib:
551 for name, value in list(attrib.items()):
552 if name and isinstance(name, str) and name[0] == '{':
553 del attrib[name]
554 attrib[name.partition('}')[2]] = value
555
538 556
539 # -------------------------------------------------------------------- 557 # --------------------------------------------------------------------
540 558
541 559
542 class ElementTree: 560 class ElementTree:
543 """An XML element hierarchy. 561 """An XML element hierarchy.
544 562
545 This class also provides support for serialization to and from 563 This class also provides support for serialization to and from
546 standard XML. 564 standard XML.
547 565
(...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after
1636 try: 1654 try:
1637 # Element is going to be shadowed by the C implementation. We need to keep 1655 # Element is going to be shadowed by the C implementation. We need to keep
1638 # the Python version of it accessible for some "creative" by external code 1656 # the Python version of it accessible for some "creative" by external code
1639 # (see tests) 1657 # (see tests)
1640 _Element_Py = Element 1658 _Element_Py = Element
1641 1659
1642 # Element, SubElement, ParseError, TreeBuilder, XMLParser 1660 # Element, SubElement, ParseError, TreeBuilder, XMLParser
1643 from _elementtree import * 1661 from _elementtree import *
1644 except ImportError: 1662 except ImportError:
1645 pass 1663 pass
OLDNEW
« no previous file with comments | « Lib/test/test_xml_etree.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+