【问题标题】:xmllint unable to read attribute valuesxmllint 无法读取属性值
【发布时间】: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


    【解决方案1】:

    替换

    ['@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
    

    输出:

    完全的

    见:Attributes in Xpath local-name()

    【讨论】:

    • 谢谢,这成功了!有关如何将 xmllint 命令定向到字符串变量而不是物理文件的任何帮助?
    • 我建议 variable="$(xmllint ... 2&gt;&amp;1)" 将 stderr 重定向到 stdout,然后重定向到您的变量。
    猜你喜欢
    • 2015-03-29
    • 2018-07-13
    • 2013-07-19
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2017-02-17
    • 2018-05-23
    相关资源
    最近更新 更多