13

I have the following XML

<process id="test">

</process>
<process id="test2">

</process>

I do get the correct values from the attributes but I want to have them separated on by a new line for further processing.

When using the command

xmlstarlet sel -t -v "count(process/@id" "example.xml)"

I can see the two are separated because it does return 2

The command used to retrieve the attributes is the following

xmlstarlet sel -t -v "process/@id" "example.xml"

Question :

Can the output be returned with a new line for each attribute? If not could it be done with xmllint?

Dany
  • 735
  • 1
  • 8
  • 27

2 Answers2

16

I was looking at this for 2 days and i found the following page:

http://www.geekfarm.org/wu/muse/XmlStarlet.html

This clarified a lot to me

I have used the following command to achieve the line breaks

xmlstarlet sel -t -m "process" -n -v "@id" "example.xml"
Dany
  • 735
  • 1
  • 8
  • 27
  • 4
    To get newlines with the `-n` flag, the `-m` option is required. I.e. when you use `sel -t -c "expression" -n some.xml`, instead use `sel -t -m "expression" -c "." -n some.xml`. – phobic Oct 14 '15 at 09:34
0

My starting point was not XML, but HTML, so I had to first convert HTML --> XML.

xmllint -html -xmlout  file.html > file.xml

Then I use the Dany's hint above:

xmlstarlet sel -t -m '//html/body/table/tr/td[2]/b/text()' -c .  -n file.xml 2> /dev/null

Then I got output on separate lines:

test
test2

Of course the xpath was different, but the goal - separate lines was achieved.

xerostomus
  • 466
  • 4
  • 11