0

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?

2
  • Please add your desired output (no description) for that sample input to your question (no comment). Commented May 21, 2020 at 14:42
  • 1
    In your sample xml, are you trying to replace warning: Line:xx with Function parameters should be aligned vertically? Commented May 21, 2020 at 14:42

1 Answer 1

1

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'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.