Iterate through XML with xmlstarlet

血红的双手。 提交于 2019-12-13 02:20:08

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/3069031/iterate-through-xml-with-xmlstarlet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!