如何解决xmllint无法读取属性值
xmllint
的专家,请帮助我基于带有属性的xpath提取XML标签值。
按如下所示对XML进行采样:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<wd:Get_Integration_Events_Response wd:version="v35.0" xmlns:wd="urn:com.workday/bsvc">
<wd:Response_Data>
<wd:Integration_Event>
<wd:Background_Process_Instance_Data>
<wd:Background_Process_Instance_Status_Reference wd:Descriptor="Completed">
<wd:ID wd:type="WID">d8b0bcd8446c11de98360015c5e6daf6</wd:ID>
**<wd:ID wd:type="Background_Process_Instance_Status_ID">Completed</wd:ID>**
</wd:Background_Process_Instance_Status_Reference>
</wd:Background_Process_Instance_Data>
</wd:Integration_Event>
</wd:Response_Data>
</wd:Get_Integration_Events_Response>
</env:Body>
</env:Envelope>
我正在尝试提取该行中突出显示的值Completed
。
没有名称空间值,命令应该是这样的:
xmllint --xpath "string(//Envelope/Body/Get_Integration_Events_Response/Response_Data/Integration_Event/Background_Process_Instance_Data/Background_Process_Instance_Status_Reference/ID[@type='Background_Process_Instance_Status_ID'])" example.xml
我的实际命令如下:
xmllint --xpath "string(//*[local-name()='Envelope' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/*[local-name()='Body']/*[local-name()='Get_Integration_Events_Response']/*[local-name()='Response_Data']/*[local-name()='Integration_Event']/*[local-name()='Background_Process_Instance_Data']/*[local-name()='Background_Process_Instance_Status_Reference']/*[local-name()='ID']['@type=Background_Process_Instance_Status_ID'])" example.xml
这是返回值d8b0bcd8446c11de98360015c5e6daf6
,而不是Completed
。
解决方法
替换
['@type=Background_Process_Instance_Status_ID']
使用
[@*[local-name()='type' and .='Background_Process_Instance_Status_ID']]
命令:
xmllint --xpath "string(//*[local-name()='Envelope' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/*[local-name()='Body']/*[local-name()='Get_Integration_Events_Response']/*[local-name()='Response_Data']/*[local-name()='Integration_Event']/*[local-name()='Background_Process_Instance_Data']/*[local-name()='Background_Process_Instance_Status_Reference']/*[local-name()='ID'][@*[local-name()='type' and .='Background_Process_Instance_Status_ID']])" example.xml
输出:
Completed
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。