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
输出:
已完成
请参阅:Xpath local-name((中的属性