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

请参阅:Attributes in Xpath local-name()