I have a XML from which I need to parse a value using Ansible.
Below is the XML:
<?xml version="1.0" encoding="utf-8"?>
<Activity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://example.com/2005/06/StandardHeader/" xmlns="http://example.com/xsd/frm/v1/Activity/ManageInternalCustomerChangeRequest">
<ActivityDetails>
<activityId>basdsds5-21aa-4ec4-b453-5f08af0fc612</activityId>
<activityType>ServiceIdRecordUpdate</activityType>
<activityStartDate>%DateTime%</activityStartDate>
<listOfActivityAttribute>
<activityAttribute>
<name>CustomerId</name>
<value>%CustomerId%</value>
</activityAttribute>
<activityAttribute>
<name>AssetInstanceId</name>
<value>%AssetInstanceId%</value>
</activityAttribute>
<activityAttribute>
<name>ServiceId</name>
<value>%ServiceId%</value>
</activityAttribute>
<activityAttribute>
<name>SupplierServiceId</name>
<value>%SupplierServiceId%</value>
</activityAttribute>
</listOfActivityAttribute>
</ActivityDetails>
</Activity>
I am able to parse and extract the value of the node activityId i.e basdsds5-21aa-4ec4-b453-5f08af0fc612 using this Ansible task
- name: Step-3:Read an element's attribute values
xml:
path: /tmp/test.xml
xpath: /x:Activity/x:ActivityDetails/x:activityId
content: text
namespaces:
x: "http://example.com/xsd/frm/v1/Activity/ManageInternalCustomerChangeRequest"
register: xmlresp
Which gives me the value
{"ansible_facts": {"Value": "basdsds5-21aa-4ec4-b453-5f08af0fc612"}, "changed": false}
However I am unable to parse or extract the value for CustomerId using the below.
I have tried prefixing the namespace across the XPath, without any more success.
- name: Step-4:Read Customer ID from XML
xml:
path: /tmp/test.xml
xpath: /x:Activity/ActivityDetails/listOfActivityAttribute/activityAttribute[name='CustomerId']/value
content: text
namespaces:
x: "http://example.com/xsd/frm/v1/Activity/ManageInternalCustomerChangeRequest"
register: xmlresp1
It fails with the error
FAILED! => {"changed": false, "msg": "Xpath /x:Activity/x:ActivityDetails/x:listOfActivityAttribute/x:activityAttribute[name='CustomerId'] does not reference a node!"}
I need to extract the value %CustomerId%, using Ansible.