mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 11:21:25 -07:00
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
This commit is contained in:
parent
26d75144b2
commit
d8429a69c5
1 changed files with 11 additions and 3 deletions
|
@ -291,7 +291,6 @@ 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
|
||||||
|
|
||||||
|
|
||||||
|
@ -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…
Add table
Add a link
Reference in a new issue