Skip to content

Commit 9e72e1d

Browse files
author
Keith Hall
committed
more fixes
1 parent edea4b2 commit 9e72e1d

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

lxml_parser.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,12 @@ def unique_namespace_prefixes(namespaces, replaceNoneWith = 'default', start = 1
304304

305305
def get_results_for_xpath_query(query, tree, context = None, namespaces = None, **variables):
306306
"""Given a query string and a document trees and optionally some context elements, compile the xpath query and execute it."""
307-
nsmap = {}
308-
if namespaces is not None:
307+
nsmap = dict()
308+
if namespaces:
309309
for prefix in namespaces.keys():
310-
if namespaces[prefix][0] != '':
311-
nsmap[prefix] = namespaces[prefix][0]
310+
nsmap[prefix] = namespaces[prefix][0]
312311

313-
try:
314-
xpath = etree.XPath(query, namespaces = nsmap)
315-
except Exception as e:
316-
raise ValueError(query) from e
312+
xpath = etree.XPath(query, namespaces = nsmap)
317313

318314
results = execute_xpath_query(tree, xpath, context, **variables)
319315
return results

xpath.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def getXPathOfNodes(nodes, args):
142142
include_indexes = not getBoolValueFromArgsOrSettings('show_hierarchy_only', args, False)
143143
unique = getBoolValueFromArgsOrSettings('copy_unique_path_only', args, True)
144144
include_attributes = include_indexes or getBoolValueFromArgsOrSettings('show_attributes_in_hierarchy', args, False)
145-
show_namespace_prefixes_from_query = getBoolValueFromArgsOrSettings('show_namespace_prefixes_from_query', args, False)
145+
show_namespace_prefixes_from_query = getBoolValueFromArgsOrSettings('show_namespace_prefixes_from_query', args, True)
146146
case_sensitive = getBoolValueFromArgsOrSettings('case_sensitive', args, True)
147147
all_attributes = getBoolValueFromArgsOrSettings('show_all_attributes', args, False)
148148

@@ -154,7 +154,7 @@ def getXPathOfNodes(nodes, args):
154154
def getTagNameWithMappedPrefix(node, namespaces):
155155
tag = getTagName(node)
156156
if show_namespace_prefixes_from_query and tag[0] is not None: # if the element belongs to a namespace
157-
unique_prefix = next((prefix for prefix in namespaces.keys() if namespaces[prefix] == (tag[0], node.prefix)), None) # find the first prefix in the map that relates to this uri
157+
unique_prefix = next((prefix for prefix in namespaces.keys() if namespaces[prefix] == (tag[0], node.prefix or '')), None) # find the first prefix in the map that relates to this uri
158158
if unique_prefix is not None:
159159
tag = (tag[0], tag[1], unique_prefix + ':' + tag[1]) # ensure that the path we display can be used to query the element
160160

@@ -858,10 +858,11 @@ def get_query_results(self, query):
858858

859859
try:
860860
results = list((result for result in get_results_for_xpath_query_multiple_trees(query, self.contexts[1], self.contexts[2])))# if not isinstance(result, etree.CommentBase)))
861-
except etree.XPathError as e:
861+
except (ValueError, etree.XPathError) as e:
862862
last_char = query.rstrip()[-1]
863863
if not last_char in ('/', ':', '@', '[', '(', ','): # log exception to console only if might be useful
864864
print('XPath: exception evaluating results for "' + query + '": ' + repr(e))
865+
#print(e.error_log)
865866
#traceback.print_tb(e.__traceback__)
866867
status_text = e.__class__.__name__ + ': ' + str(e)
867868

@@ -1075,7 +1076,7 @@ def completions_functions():
10751076
try:
10761077
completion_contexts = get_results_for_xpath_query(query, tree, None, namespaces[tree.getroot()], **xpath_variables)
10771078
# TODO: if result is not a node, break out as we can't offer any useful suggestions (currently we just get an exception: Non-Element values not supported at this point - got 'example string') when it tries $expression_contexts/*
1078-
except etree.XPathError as e: # xpath query invalid, just show static contexts
1079+
except (ValueError, etree.XPathError) as e: # xpath query invalid, just show static contexts
10791080
completion_contexts = None
10801081
print('XPath: exception obtaining completions for subquery "' + query + '": ' + repr(e))
10811082
break
@@ -1087,8 +1088,11 @@ def completions_functions():
10871088
ns_prefix = ''
10881089
if ns is not None: # ensure we get the prefix that we have mapped to the namespace for the query
10891090
root = result.getroottree().getroot()
1090-
ns_prefix = next((nsprefix for nsprefix in namespaces[root].keys() if namespaces[root][nsprefix] == (ns, result.prefix))) # find the first prefix in the map that relates to this uri
1091-
fullname = ns_prefix + ':' + localname
1091+
ns_prefix = next((nsprefix for nsprefix in namespaces[root].keys() if namespaces[root][nsprefix] == (ns, result.prefix or '')), None) # find the first prefix in the map that relates to this uri
1092+
if ns_prefix:
1093+
fullname = ns_prefix + ':' + localname
1094+
else:
1095+
print('XPath warning: unable to find', ns, result.prefix, ' in root namespaces', namespaces[root], 'while generating completions')
10921096
if not last_location_step.endswith(':') or last_location_step.endswith('::') or last_location_step.endswith(ns_prefix + ':'): # ensure `prefix :` works correctly and also `different_prefix_to_suggestion:` (note that we don't do this for attributes - attributes are not allowed spaces before the colon, and if the prefix differs when there is no space, Sublime will replace it with the completion anyway)
10931097
completion = fullname
10941098
else:

xpath.sublime-settings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// default namespace prefix for xpath query when elements have xmlns attribute set
2222
"default_namespace_prefix": "default",
2323
// whether or not to show the namespace prefix that the xpath query will expect
24-
"show_namespace_prefixes_from_query": false,
24+
"show_namespace_prefixes_from_query": true,
2525
// whether or not to only show the current xpath in the status bar if the view is not dirty. Useful to save CPU cycles when editing a document
2626
"only_show_xpath_if_saved": false,
2727
// only show the first x number of results from the xpath query, to speed up result display. Set to <= 0 for no limit

0 commit comments

Comments
 (0)