如何从soap响应中解析xpath以检索键值对

问题描述 投票:0回答:1

使用 bash,我尝试从下面的肥皂响应中提取 id 和 name 作为键值对,并希望将此对存储在 csv 文件中。 下面的 xmllint 命令返回不需要的值,因为我无法提供过滤器。 任何人都可以帮忙吗? 我愿意使用 xmllint 和 xmlstarlet。

我尝试过的 xmllint 命令:

xmllint --xpath "//*[local-name()='getAllUserApplicationsResponse']/*[local-name()='return']/*[local-name()='name']/text()" request.xml
要提取的元素:

<ax2159:id>396112</ax2159:id> <ax2159:name>com.example.app.name</ax2159:name>

请注意,<ns:return></ns:return>

是重复元素。

肥皂反应:

<ns:getAllUserApplicationsResponse xmlns:ns="http://application.app.api.admin.abc.example.com" xmlns:ax2179="http://logging.app.api.admin.abc.example.com/xsd" xmlns:ax2169="http://stagingarea.app.api.admin.abc.example.com/xsd" xmlns:ax2159="http://types.core.api.admin.abc.example.com/xsd" xmlns:ax2171="http://permission.common.app.api.admin.abc.example.com/xsd" xmlns:ax2161="http://application.app.api.admin.abc.example.com/xsd" xmlns:ax2183="http://component.app.api.admin.abc.example.com/xsd" xmlns:ax2173="http://resinstance.app.api.admin.abc.example.com/xsd" xmlns:ax2165="http://status.app.api.admin.abc.example.com/xsd" xmlns:ax2176="http://svars.app.api.admin.abc.example.com/xsd" xmlns:ax2187="http://binding.app.api.admin.abc.example.com/xsd" xmlns:ax2155="http://exception.core.api.admin.abc.example.com/xsd" xmlns:ax2156="http://fixer.core.api.admin.abc.example.com/xsd"> <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax2161:ApplicationShortDesc"> <ax2159:id>396112</ax2159:id> <ax2159:name>com.example.app.name</ax2159:name> <ax2161:environment xsi:type="ax2159:EntityIdentifier"> <ax2159:id>6175</ax2159:id> <ax2159:name>app_environment</ax2159:name> </ax2161:environment> <ax2161:folder xsi:type="ax2159:EntityIdentifier"> <ax2159:id>34148</ax2159:id> <ax2159:name>MYAPP</ax2159:name> </ax2161:folder> <ax2161:runtimeState>Running</ax2161:runtimeState> <ax2161:runtimeStateEnum>RUNNING</ax2161:runtimeStateEnum> </ns:return> </ns:getAllUserApplicationsResponse>
    
xml bash xml-namespaces xmlstarlet xmllint
1个回答
0
投票
对于此任务,您可以考虑使用

xq。它可以读取 XML 并写入 CSV。

xq -r '."ns:getAllUserApplicationsResponse"."ns:return" | arrays[] // . | [."ax2159:id", ."ax2159:name"] | @csv ' request.xml
"396112","com.example.app.name"
    
© www.soinside.com 2019 - 2024. All rights reserved.