0

I've tried xmlstarlet for processing an individual .xhtml files.

xmlstarlet fo --omit-decl --recover --html file.xhtml 

This command process an individual files and prints output on terminal only. How to batch process multiple files and save results on stdout as a files? (with same name, by adding prefix or suffix for files)

2
  • You could use the code I added to my answer to your previous question (on your request in the comments there) and just change the xmlstarlet command to what you show here. Did you try that? What issues did you run into when you tried? Do you want me to repeat the code in an answer here? Commented Oct 3, 2021 at 11:30
  • No, first I need normalize all .xhtml files with command above, as xmlstarlet can deal only with properly formatted XML documents. Without first formatting .xhtml docs, xmlstarlet just print a plenty warnings "Opening and ending tag mismatch:", "Premature end of data in tag","Warning: unrecognized option '--in-place', Segmentation fault". Commented Oct 3, 2021 at 12:08

1 Answer 1

1

The code below is an only slightly modified variant from the end of my answer to your previous question on the same topic, utilizing identical ways of doing the actual batch processing.

If the files that you want to process all match the pattern ./*.xhtml, i.e., if they have a .xhtml filename suffix and are in the current directory, then you would be able to process all those files with either of the above commands using a simple shell loop.

for name in ./*.xhtml; do
        xmlstarlet fo --omit-decl --recover --html "$name" >"$name".new
done

The command in the loop outputs the resulting document to files with the same name as the old files but with a .new filename suffix appended.

To run the above on all XHTML files in a directory hierarchy, i.e., in a directory with several subdirectories, you may use find.

find . -type f -name '*.xhtml' -exec sh -c '
        for name do
                xmlstarlet fo --omit-decl --recover --html "$name" >"$name".new
        done' sh {} +
1
  • Yes, this works as expected. Thanks! Commented Oct 3, 2021 at 22:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.