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?
The xpath expression is incorrect. Each element should be prefixed with the namespace /x:web-app/x:security-constraint/x:web-resource-collection/x:web-resource-name
- xml:
path: xml-with-namespace.xml"
xpath: /x:web-app/x:security-constraint/x:web-resource-collection/x:web-resource-name
content: text
namespaces:
x: "http://xmlns.jcp.org/xml/ns/javaee"
register: xmlresp
- debug:
var: xmlresp
See community.general.xml – Manage bits and pieces of XML files or strings — Ansible Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With