XML module xpath issues - "does not reference a node" error

767 views
Skip to first unread message

ADB101

unread,
Feb 19, 2018, 7:57:26 AM2/19/18
to Ansible Project
Hi All.

I am having issues getting the "xml" module to accept what I believe to be valid xpaths, it just returns "does not reference a node". I have used an online xpath checker (https://www.freeformatter.com/xpath-tester.html) which, when I use the target xml file, gives me the correct data. The xml file is from an Oracle middleware product so I can't template it and I have to make the config changes directly in the xml file.

I can get some xpath's to work correctly, but only in "simple" (?) forms. For example:

- name: Check current value
  xml:
    file: "{{oam_xml_file}}"
    xpath: /x:Configuration/Setting[@Name='NGAMConfiguration']/Setting[@Name='DeployedComponent']/Setting[@Name='Agent']/Setting[@Name='WebGate']/Setting[@Name='Instance']/Setting[@Name='APPS_OAM11G']/Setting[@Name='UserDefinedParameters']/Setting[@Name='client_request_retry_attempts']/*
    content: text
    namespaces:
       x: http://www.w3.org/2001/XMLSchema


fails with "does not reference a node" (but works fine when checked using the online xpath checker) but:

- name: Check current value
  xml:
    file: "{{oam_xml_file}}"
    xpath: //x:Setting[@Name='client_request_retry_attempts']/*
    content: text
    namespaces:
       x: http://www.w3.org/2001/XMLSchema

returns the value I am expecting. (Note: I could only get the short version working by adding the "namespaces" option).

The issue is that I really need the full xpath to work as there are often several entries with the same "Name='tag'" that are only distinguishable by their full path. I have tried even going one level down in the xml, using a full path, and I still get "does not reference a node" error message.

The original xml is a bit of a beast so I have put a copy here rather than paste it in.

I'm running Ansible 2.2.1 so I'm using a version of the "xml" module before it was bundled with Ansible 2.4. For various reasons I have to stick with using version 2.2.1 for the time being so I was hoping someone could either inform me how to make full path xpaths work (or tell me that I'm out of luck and have to wait until we can upgrade to Ansible 2.4!)

Many thanks,
Aidan

Kai Stian Olstad

unread,
Feb 19, 2018, 8:38:08 AM2/19/18
to ansible...@googlegroups.com
On 19.02.2018 13:57, ADB101 wrote:
> I can get some xpath's to work correctly, but only in "simple" (?)
> forms.
> For example:
>
> - name: Check current value
> xml:
> file: "{{oam_xml_file}}"
> xpath:
> /x:Configuration/Setting[@Name='NGAMConfiguration']/Setting[@Name='DeployedComponent']/Setting[@Name='Agent']/Setting[@Name='WebGate']/Setting[@Name='Instance']/Setting[@Name='APPS_OAM11G']/Setting[@Name='UserDefinedParameters']/Setting[@Name='client_request_retry_attempts']/*
> content: text
> namespaces:
> x: http://www.w3.org/2001/XMLSchema
>
> The issue is that I really need the full xpath to work as there are
> often
> several entries with the same "Name='*tag*'" that are only
> distinguishable
> by their full path. I have tried even going one level down in the xml,
> using a full path, and I still get "does not reference a node" error
> message.

I'm not sure, but my hunch is that you need to postfix all with the
namespace, so /x:Settings.... on all of them.

--
Kai Stian Olstad

ADB101

unread,
Feb 19, 2018, 10:44:38 AM2/19/18
to Ansible Project
Kai,

Genius! That sorted it.

Thanks. :)
Reply all
Reply to author
Forward
0 new messages