diff options
author | Lance Stout <lancestout@gmail.com> | 2013-01-20 15:43:02 -0800 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2013-01-20 15:43:02 -0800 |
commit | ccf7916257d6af6ec9fd76d4c4c438b7f9d8078c (patch) | |
tree | a4f84f95af01cb3f5ee0b004d65e900ff657b184 /sleekxmpp/xmlstream | |
parent | d86adfa1b164293562e8b86dae7a82755b95626d (diff) | |
download | slixmpp-ccf7916257d6af6ec9fd76d4c4c438b7f9d8078c.tar.gz slixmpp-ccf7916257d6af6ec9fd76d4c4c438b7f9d8078c.tar.bz2 slixmpp-ccf7916257d6af6ec9fd76d4c4c438b7f9d8078c.tar.xz slixmpp-ccf7916257d6af6ec9fd76d4c4c438b7f9d8078c.zip |
Allow for simplified XPath namespaces
Diffstat (limited to 'sleekxmpp/xmlstream')
-rw-r--r-- | sleekxmpp/xmlstream/matcher/xpath.py | 37 | ||||
-rw-r--r-- | sleekxmpp/xmlstream/stanzabase.py | 2 |
2 files changed, 6 insertions, 33 deletions
diff --git a/sleekxmpp/xmlstream/matcher/xpath.py b/sleekxmpp/xmlstream/matcher/xpath.py index 3f03e68e..f3d28429 100644 --- a/sleekxmpp/xmlstream/matcher/xpath.py +++ b/sleekxmpp/xmlstream/matcher/xpath.py @@ -9,16 +9,10 @@ :license: MIT, see LICENSE for more details """ -from sleekxmpp.xmlstream.stanzabase import ET +from sleekxmpp.xmlstream.stanzabase import ET, fix_ns from sleekxmpp.xmlstream.matcher.base import MatcherBase -# Flag indicating if the builtin XPath matcher should be used, which -# uses namespaces, or a custom matcher that ignores namespaces. -# Changing this will affect ALL XPath matchers. -IGNORE_NS = False - - class MatchXPath(MatcherBase): """ @@ -38,6 +32,9 @@ class MatchXPath(MatcherBase): expressions will be matched without using namespaces. """ + def __init__(self, criteria): + self._criteria = fix_ns(criteria) + def match(self, xml): """ Compare a stanza's XML contents to an XPath expression. @@ -59,28 +56,4 @@ class MatchXPath(MatcherBase): x = ET.Element('x') x.append(xml) - if not IGNORE_NS: - # Use builtin, namespace respecting, XPath matcher. - if x.find(self._criteria) is not None: - return True - return False - else: - # Remove namespaces from the XPath expression. - criteria = [] - for ns_block in self._criteria.split('{'): - criteria.extend(ns_block.split('}')[-1].split('/')) - - # Walk the XPath expression. - xml = x - for tag in criteria: - if not tag: - # Skip empty tag name artifacts from the cleanup phase. - continue - - children = [c.tag.split('}')[-1] for c in xml] - try: - index = children.index(tag) - except ValueError: - return False - xml = list(xml)[index] - return True + return x.find(self._criteria) is not None diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 122d7eb4..120373db 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -192,7 +192,7 @@ def fix_ns(xpath, split=False, propagate_ns=True, default_ns=''): for element in elements: if element: # Skip empty entry artifacts from splitting. - if propagate_ns: + if propagate_ns and element[0] != '*': tag = '{%s}%s' % (namespace, element) else: tag = element |