【发布时间】:2021-02-01 04:09:42
【问题描述】:
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。
【问题讨论】:
标签: xml parsing xpath xml-namespaces xmllint