0

I have a very big xml with no root node (oversimplified example):

<address>1</address>
<address>2</address>
<address>3</address>
<address>4</address>

I have tried to add the root node inside xmlstarlet to select and compute some data this way:

 xmlstarlet -q fo -R file.xml | xmlstarlet sel -t -v "count(//address)" 

seems to work but removing all but first node, so calculation is not working:

<?xml version="1.0"?>
<address>1</address>
Forge
  • 1,587
  • 1
  • 15
  • 36

1 Answers1

0

The HTML option, -H,

xmlstarlet fo -o -R -H -D file.xml

does better at recovery here as it wraps your rootless elements in html and body elements:

<html>
  <body><address>1</address>
<address>2</address>
<address>3</address>
<address>4</address>
</body>
</html>

Then your count() call,

xmlstarlet fo -o -R -H -D file.xml | xmlstarlet sel -t -v "count(//address)" 

will return 4 as expected.

References

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • sadly, this is not working with my real xml as some errors arised using HTML ( Tag address-request invalid – Forge Feb 08 '21 at 14:09
  • See the extensive recommendations for dealing with malformed XML in the reference I provided in the answer. If you're still stuck after that, post a new question with a representative [mcve] reflecting the new problem. – kjhughes Feb 08 '21 at 15:19