The most simple solution to remove all comments from a textfile I could come up with is:
sed 's/<!--/\x0<!--/g;s/-->/-->\x0/g' | grep -zv '^<!--' | tr -d '\0'
To explain:
The sed
will put in a null
char like this:
<Table>
\0<!--
to be removed bla bla bla bla bla bl............
removeee
to be removeddddd
-->\0
<row>
<column name="example" value="1" ></column>
</row>
</Table>
Than the grep -z
will treat that character as "line separator"
<Table>\n
<!--\n to be removed bla bla bla bla bla bl............\n\n removeee\n\n to be removeddddd\n -->
\n\n<row>\n <column name="example" value="1" ></column>\n </row>\n</Table>\n
grep -v
will remove the middle part.
And finally tr -d
will remove the \0
again.
In this case it should be applied to both files before comparing e.g.:
diff <(sed 's/<!--/\x0<!--/g;s/-->/-->\x0/g' file1.xml | grep -zv '^<!--' | tr -d '\0') <(sed 's/<!--/\x0<!--/g;s/-->/-->\x0/g' file2.xml | grep -zv '^<!--' | tr -d '\0')
or more readable with a function:
stripcomments() {cat "$@" | sed 's/<!--/\x0<!--/g;s/-->/-->\x0/g' | grep -zv '^<!--' | tr -d '\0'}
diff <(stripcomments file1.xml) <(stripcomments file2.xml)
In theory there might be some issues with CDATA blocks, as they can be used to have unbalanced comments, and there is a higher probability of them having important null-characters, but I have never seen such an xml file in real life.
So for most valid xml-files this should work.
tidy
or write an identity xslt.tidy
. Read my comment on @glennjackman answer