我有一個需要從 XML 檔案中讀取元素的要求。檔案中的內容具有 SOAP 內容。從檔案中讀取元素時,定義的 XPath 給出如下錯誤:
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 任務:
- 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 檔案:
<?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>
我需要得到價值"STATE=STOPPED------COMMENTS=Cant startup as Ayehu did not shutdown this database"。
更新:
這是ansible更新的代碼:
- 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
錯誤:
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)"}
uj5u.com熱心網友回復:
三個問題:
- XPath 運算式中不應有尾隨
/。 - 每個元素名稱應該只有一個命名空間前綴,而不是兩個。
- 命名空間前綴
z應該被y賦予你的 Ansible 宣告。
根據您的 Ansible 代碼定義的命名空間前綴,
namespaces:
x: "http://schemas.xmlsoap.org/soap/envelope/"
y: "http://WebService.domain.com/"
改變
/x:soap:Envelope/x:soap:Body/z:WebService_Response/z:WebService_GenericResult/
到
/x:Envelope/x:Body/y:WebService_Response/y:WebService_GenericResult
請注意洗掉的尾隨/、洗掉的soap命名空間前綴和更改的z命名y空間前綴。
您可能希望對肥皂信封使用比x( soapenv, env,soap等) 更常規的前綴,但命名空間前綴是任意的,只要它的使用匹配其宣告的與重要部分的系結,即命名空間 URI 值本身。
也可以看看
- 為 XML 命名空間前綴使用不同的名稱是否重要?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/429365.html
