I have a junit report XML file like this:
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite>
<testcase classname='Formatting Test' name='Test.swift'>
<failure message='Function parameters should be aligned vertically'>warning: Line:39 </failure>
</testcase>
<testcase classname='Formatting Test' name='Test.swift'>
<failure message='Function parameters should be aligned vertically'>warning: Line:40 </failure>
</testcase>
</testsuite>
</testsuites>
I want to replace content of <failure>
tag with its message
attribute using XMLStarlet. I found a similar question about updating with xpath expression: SO question
but if I try any of those commands:
xmlstarlet edit -u '//testsuites/testsuite/testcase/failure' -x '@message/text()' test.xml
xmlstarlet edit -u '//testsuites/testsuite/testcase/failure' -x '@message' test.xml
then the <failure
> value is completely gone. If I use this (just to test):
xmlstarlet edit -u '//testsuites/testsuite/testcase/failure' -x '../@name' test.xml
the result will be:
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite>
<testcase classname="Formatting Test" name="Test.swift">
<failure message="Function parameters should be aligned vertically" name="Test.swift"/>
</testcase>
<testcase classname="Formatting Test" name="Test.swift">
<failure message="Function parameters should be aligned vertically" name="Test.swift"/>
</testcase>
</testsuite>
</testsuites>
It appends name
attribute to <failure>
tag! It's so confusing how the xpath works for -x
parameter. So how to replace the content of <failure>
tag with its attribute value?
I think you're close but you're selecting an attribute node. This is why the name
attribute is added in your last command line example.
To use the value of the attribute convert it to a string with string()
.
Try this updated command line:
xmlstarlet edit -u '//testsuites/testsuite/testcase/failure' -x 'string(@message)' test.xml
Note: You can probably also shorten your -u
to just '//failure'
.