[英]How to extract value from XML payload using Shell script
I have runtime XML file which has predefined format and Its being created in Jenkins workspace.我有运行时 XML 文件,它具有预定义的格式并且它是在 Jenkins 工作区中创建的。 I just want to parse the XML payload by using shell script.
我只想使用 shell 脚本解析 XML 负载。
Sample Payload:示例有效负载:
<test-results>
<test-method status="FAIL" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">
<params>
<param index="0">
<value>
<![CDATA[ account_number:22988419 ]]>
</value>
</param>
<param index="1">
<value>
<![CDATA[ txn_id:6wdadfsad2134330L ]]>
</value>
</param>
<param index="2">
<value>
<![CDATA[ amount:1100 ]]>
</value>
</param>
<param index="3">
<value>
<![CDATA[ currency:USD ]]>
</value>
</param>
<param index="4">
<value>
<![CDATA[Id:11a09 ]]>
</value>
</param>
<param index="5">
<value>
<![CDATA[Name:Consumer [testId=AS1-TC2, description=Txn amount; wallet -Bal, BA,CC,VISA Credit; Consumer - CC;No other preference set]]]>
</value>
</param>
</params>
</test-method>
<test-method status="PASS" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">
<params>
<param index="0">
<value>
<![CDATA[ account_number:22988419 ]]>
</value>
</param>
<param index="1">
<value>
<![CDATA[ txn_id:6wdadfsad2134330L ]]>
</value>
</param>
</params>
</test-method>
<test-method status="FAIL" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">
<params>
<param index="0">
<value>
<![CDATA[ account_number:22988419 ]]>
</value>
</param>
<param index="1">
<value>
<![CDATA[Name:Consumer [testId=AS1-TC3, description=Txn amount; wallet -Bal, BA,CC,VISA Credit; Consumer - CC;No other preference set]]]>
</value>
</param>
</params>
</test-method>
</test-results>
The above payload I have to read the status of test-method if it's "FAIL" then I need to get the "testId" value from that particular test method.上面的有效负载如果“失败”,我必须读取测试方法的状态,然后我需要从该特定测试方法中获取“testId”值。 The above payload I have 3 test methods only two had status Failed I need to fetch both Test id and assign to variable as like below
上面的有效负载我有 3 个测试方法,只有两个状态为 Failed 我需要获取两个测试 ID 并分配给变量,如下所示
Expected Output:预期输出:
fetchResult = AS1-TC2,AS1-TC3
I just need to fetch the "testId" of failed test-methods and assign it to variable with comma separated using shell script.我只需要获取失败的测试方法的“testId”,并将其分配给使用 shell 脚本以逗号分隔的变量。
I tried the below lines but It doesn't return the entire test-method tags我尝试了以下几行,但它没有返回整个测试方法标签
failedTC=`grep "test-method.*FAIL" results.xml | sed -e 's/^.*test-instance-name="(.*)-----.*/\1/'
Output:输出:
<test-method status="FAIL" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">
I want to return entire <test-method> ... </test-method>
for status="FAIL"我想为 status="FAIL" 返回整个
<test-method> ... </test-method>
Any leads....任何线索....
You could use the following xmlstarlet and sed command:您可以使用以下 xmlstarlet 和 sed 命令:
xmlstarlet sel -T -t -c "test-results/test-method[@status='FAIL']/params/param/value[contains(.,'testId')]" file | sed -n 's/.*testId=\([^,]\+\),.*/\1/p'
-T
: raw test (without XML node). -T
:原始测试(没有 XML 节点)。
-t
: template -t
: 模板-c
: Xpath expression -c
: Xpath 表达式
[@status='FAIL']
is the test on the attribute status
. [@status='FAIL']
是对属性status
的测试。
value[contains(.,'testId')]
tests if the value
node contains that specific string. value[contains(.,'testId')]
测试value
节点是否包含该特定字符串。
The sed
command extracts the wanted string. sed
命令提取所需的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.