Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "does not reference a node" with XML document with namespaces using Ansbile XML module?

Tags:

xml

xpath

ansible

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?

like image 237
onknows Avatar asked Feb 02 '26 11:02

onknows


1 Answers

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

like image 69
onknows Avatar answered Feb 04 '26 02:02

onknows



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!