Given the following XML document
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<security-constraint>
<web-resource-collection>
<web-resource-name>Restricted URLs</web-resource-name>
<url-pattern>/</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
I can for example use Ansible xml module to get the text Restricted URLs
- xml:
path: "simple.xml"
xpath: /web-app/security-constraint/web-resource-collection/web-resource-name
content: text
register: xmlresp
- debug:
var: xmlresp
When I add a namespace to the xml for example
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee">
<security-constraint>
<web-resource-collection>
<web-resource-name>Restricted URLs</web-resource-name>
<url-pattern>/</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
and change Ansible YAML code to
- xml:
path: xml-with-namespace.xml"
xpath: /x:web-app/security-constraint/web-resource-collection/web-resource-name
content: text
namespaces:
x: "http://xmlns.jcp.org/xml/ns/javaee"
register: xmlresp
- debug:
var: xmlresp
This produces error message
TASK [centos : xml] ******************************************************** fatal: [centos]: FAILED! => {"changed": false, "msg": "Xpath /x:web-app/security-constraint/web-resource-collection/web-resource-name does not reference a node!"}
Why is this?