开发者

Iterate through XML with xmlstarlet

开发者 https://www.devze.com 2023-01-03 18:22 出处:网络
I have the following XML: <?xml version=\"1.0\" encoding=\"UTF-8\"?> <test-report> <testsuite>

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<test-report>
<testsuite>
<test name="RegisterConnection1Tests">
<testcase name="testRregisterConnection001"></testcase>
<testcase name="testRegisterConnection002"></testcase>
</test>开发者_如何学运维
<test name="RegisterConnection2Tests">
<testcase name="testRregisterConnection001"></testcase>
<testcase name="testRegisterConnection002"></testcase>
</test>
</testsuite>
</test-report>

And I want the output:

RegisterConnection1Tests,testRregisterConnection001
RegisterConnection1Tests,testRregisterConnection002
RegisterConnection2Tests,testRregisterConnection001
RegisterConnection2Tests,testRregisterConnection002

I'm confused as to how to show the children as I expected

xmlstarlet sel -t -m 'test-report/testsuite/test' -v '@name' -v '//testcase/@name' -n $1 to work, though it only inputs:

RegisterConnection1TeststestRregisterConnection001
RegisterConnection2TeststestRregisterConnection001


To add the missing comma you can add another -v "','" In your second column you are selecting with an absolute xpath expression from the root element and not from the element matched by the template, the double slashes are wrong. Since you want one line per testcase I would iterate over the testcase elements and then add the name attribute of the parent element like this:

xmlstarlet sel -t -m 'test-report/testsuite/test/testcase' -v '../@name' -v "','" -v '@name' -n $1
0

精彩评论

暂无评论...
验证码 换一张
取 消