Search code examples
xmlxpathxmllint

Merging different numbers of Xpath nodes using xmllint


I have a file having data as follows:

       <root>
         <x>
            <y lang="en">
              <z>1</z>
              <z>2</z>
              <z>3</z>
              <z>4</z>
              <t>5</t>
            </y>
          </x>
          <x>
            <y lang="en">
              <z>a</z>
              <z>b</z>
              <t>c</t>
            </y>
          </x>
       </root>

I would like to print them as follows

1 2 3 4 5

a b c

The first one has 4z and 1t and the second one has 2z and 1t. Since xargs is not static I can not print them as I want. I tried a script as follows $f being the file:

xmllint --xpath "//root/x/y/z/node() | //root/x/y/t/node()" $f | xargs -n2

What I get as a result is:

1 2

3 4

5 a

b c

If you would have any ideas I would be happy.


Solution

  • Try:

    xidel file.xml -e '//x/string-join(./y/*/.," ")'
    

    The output I get with your xml:

    1 2 3 4 5
    a b c