xml module: Parse and evaluate xpath on input (#28591)

* xml module: Parse and evaluate xpath on input

This fixes cmprescott/ansible-xml#68

* Update error string
pull/28595/head
Dag Wieers 7 years ago committed by GitHub
parent 26d75144b2
commit d8429a69c5

@ -291,8 +291,7 @@ def xpath_matches(tree, xpath, namespaces):
""" Test if a node exists """ """ Test if a node exists """
if tree.xpath(xpath, namespaces=namespaces): if tree.xpath(xpath, namespaces=namespaces):
return True return True
else: return False
return False
def delete_xpath_target(module, tree, xpath, namespaces): def delete_xpath_target(module, tree, xpath, namespaces):
@ -708,12 +707,21 @@ def main():
else: else:
module.fail_json(msg="The target XML source '%s' does not exist." % xml_file) module.fail_json(msg="The target XML source '%s' does not exist." % xml_file)
# Parse and evaluate xpath expression
if xpath:
try:
etree.XPath(xpath)
except etree.XPathSyntaxError as e:
module.fail_json(msg="Syntax error in xpath expression: %s (%s)" % (xpath, e))
except etree.XPathEvalError as e:
module.fail_json(msg="Evaluation error in xpath expression: %s (%s)" % (xpath, e))
# Try to parse in the target XML file # Try to parse in the target XML file
try: try:
parser = etree.XMLParser(remove_blank_text=pretty_print) parser = etree.XMLParser(remove_blank_text=pretty_print)
doc = etree.parse(infile, parser) doc = etree.parse(infile, parser)
except etree.XMLSyntaxError as e: except etree.XMLSyntaxError as e:
module.fail_json(msg="Error while parsing path: %s" % e) module.fail_json(msg="Error while parsing document: %s (%s)" % (xml_file or 'xml_string', e))
# Ensure we have the original copy to compare # Ensure we have the original copy to compare
if module._diff: if module._diff:

Loading…
Cancel
Save