xmlpath 和一个或多个 xmlstarlet 命令系列,用于根据重复 parent 元素的实例编辑 XML child 元素值

xmlpath and one or more series of xmlstarlet commands to edit XML child element value based on instances of repeated parent element

输入xml:

 <?xml version="1.0" encoding="utf-8"?>
 <testsuites>
    <testsuite>
        <testcase classname='Formatting Test' name='Test_01.swift'>
            <failure message='Function parameters should be aligned vertically'>warning: Line:10 </failure>
        </testcase>
        <testcase classname='Formatting Test' name='Test_02.swift'>
            <failure message='Function parameters should be aligned vertically'>warning: Line:11 </failure>
        </testcase>
    </testsuite>
</testsuites>

输出:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
    <testsuite>
        <testcase classname='Formatting Test' name='Test_01.swift'>
            <failure message='Function parameters should be aligned vertically'>debug_line_1 ></failure>
        </testcase>
        <testcase classname='Formatting Test' name='Test_02.swift'>
            <failure message='Function parameters should be aligned vertically'>debug_line_2 ></failure>
        </testcase>
    </testsuite>
</testsuites>

我知道这会得到节点元素测试用例的数量:

`xmlstarlet sel  -t -c "count(//*/testcase)" filename.xml`

在 bash 上面的行会 return: 2

我需要一个 xpath(或者更多,如果不能通过一次)表达式,我可以用一个函数来替换 child 节点元素值失败,根据实例进行硬编码parent 测试用例。例如(根据输出):

实例 1

测试用例类名='Formatting Test'名称='Test_01.swift'

原值

失败信息='Function parameters should be aligned vertically'

警告:Line:10

新值将为:

debug_line_1

对于所有连续的节点元素testcase其children值应该增加1:

下一个测试用例元素值为

debug_line_2

等等...

感谢任何帮助。

它不是很有效,但你可以计算前面的 testcase 个元素...

xmlstarlet ed -u '//testcase/*' -x 'concat("debug_line_",count(preceding::testcase)+1)' input.xml