我使用Ansible来获取case值"7.01.12 (125)“中的元素值。我收到错误:没有引用节点!下面是我的XML:
<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ZSI="http://www.zolera.com/schemas/ZSI/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header/>
<SOAP-ENV:Body xmlns:ns1="urn:zola2">
<ns1:getVersionResponse>
<Result>7.01.12 (125)</Result>
</ns1:getVersionResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>以下是xml攻略部分:
- name: XML
xml:
xmlstring: "{{ version.content }}"
namespaces:
ns1: urn:zola2
SOAP-ENV: http://schemas.xmlsoap.org/soap/envelope/
xpath: //SOAP-ENV:Body/ns1:getVersionResponse/Result/text()
content: text
register: answer
- debug:
var: answer.matches通过从xpath中删除文本(),我不会得到错误和结果=“answer.matches”:"7.01.12 (125)“,我只需要值"7.01.12 (125)”。我在xpath测试器/计算器上测试了我的XPath,得到了正确的结果。我在这里错过了什么?提前谢谢。
发布于 2020-04-02 14:15:54
对不起,看起来这只是xml模块对xpath:的看法--它总是会产生一个Node或Element,而这就是将出现在该register的matches列表中的内容
但是,您可以从匹配的元素中提取值,而不必通过dict2items两次指定元素名称
- xml:
# as you have it
register: answer
- set_fact:
the_text: >-
{{ answer.matches[0]
| dict2items
| map(attribute="value")
| first }}https://stackoverflow.com/questions/60978097
复制相似问题