简体   繁体   中英

XPath syntax error via Ansible playbook for SOAP XML

I have a requirement where I need to read an element from a XML file. The content in file has SOAP content. While reading the element from the file, the XPath defined is giving the error as below:

 fatal: [localhost]: FAILED: => {"changed", false: "msg": "Syntax error in xpath expression: `/x:soap:Envelope/x:soap:Body/z:WebService_Response/z:WebService_GenericResult/` (Invalid expression)"}

Ansible tasks:

- name: read value from the ayehu xml
  xml:
    path: ./ayehu_rest_out.xml
    xpath: /x:soap:Envelope/x:soap:Body/y:WebService_Response/y:WebService_GenericResult/
    content: text
    namespaces:
            x: "http://schemas.xmlsoap.org/soap/envelope/"
            y: "http://WebService.domain.com/"
  register:  xml_value_content
- debug:
      var:  xml_value_content

XML file:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http:/ /www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <WebService_Response xmlns="http://WebService.domain.com/">
         <WebService_GenericResult>
      STATE=STOPPED------COMMENTS=Cant startup as Ayehu did not shutdown this database
         </WebService_GenericResult>
      </WebService_Response>
   </soap:Body>
</soap:Envelope>

I need to get the value "STATE=STOPPED------COMMENTS=Cant startup as Ayehu did not shutdown this database" .

Update:

This is the ansible updated code:

- name: read value from the ayehu xml
  xml:
    path: ./ayehu_rest_out.xml
    xpath: /env:Envelope/env:Body/y:WebService_Response/y:WebService_GenericResult/
    content: text
    namespaces:
           env: "http://schemas.xmlsoap.org/soap/envelope/"
           y: "http://WebService.domain.com/"
  register:  xml_value_content

- debug:
    var:  xml_value_content

Error:

 TASK [read value from the ayehu xml] ****************************************************** fatal: [localhost]: FAILED: => {"changed", false: "msg": "Syntax error in xpath expression: /env:Envelope/env:Body/y:WebService_Response/y:WebService_GenericResult/ (Invalid expression)"}

Three problems:

  1. There should be no trailing / in the XPath expression.
  2. There should be only a single namespace prefix per element name, not two.
  3. Namespace prefix z should be y given your Ansible declarations.

Based on your Ansible Code defined namespace prefixes,

namespaces:
        x: "http://schemas.xmlsoap.org/soap/envelope/"
        y: "http://WebService.domain.com/"

change

/x:soap:Envelope/x:soap:Body/z:WebService_Response/z:WebService_GenericResult/ 

to

/x:Envelope/x:Body/y:WebService_Response/y:WebService_GenericResult 

Note the dropped trailing / , the dropped soap namespace prefixes, and the z to y changed namespace prefixes.

You might want to use a more conventional prefix for the soap envelope than x ( soapenv , env , soap , etc), but the namespace prefix is arbitrary as long as its use matches its declared binding to the important part, the namespace URI value itself.

See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM