Search code examples
xmlxmlstarlet

xmlstarlet - order alternate pairs of tag values


With the this XML

<a>
<b>1</b>
<c>2</c>
<b>3</b>
<c>4</c>
</a>

I would like this output from xmlstarlet:

1 2
3 4

But with this command:

xml sel -T -t -m /a -v b -v c -n

I get:

1
32
4

Solution

  • Ok, I was making it too complicated:

    xml sel -T -t -m /a/* -v . -o " " -i "name()=\"c\"" -n -b test.xml
    

    So that's: always print out the value of the child of a, print a space, and if this tag is c then print a new line.