web-dev-qa-db-ja.com

XMLファイルからコメントを削除する方法は?

XMLファイルからコメントを削除できるコマンドラインツールはありますか?または、XMLパーサーを使用してこれを行う小さなプログラムを作成する必要がありますか?

Update:考えられるすべてのXMLファイルのサブセットのみを処理するソリューションには興味がありません。

たとえば、正規表現はXML解析を処理できません。

https://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la

5
Erik Sjölund

私はこの方法でそれをします:

cat myfile.xml | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d' > cleaned.xml

または:

awk 'in_comment&&/-->/{sub(/([^-]|-[^-])*--+>/,"");in_comment=0}
 in_comment{next}
 {gsub(/<!--+([^-]|-[^-])*--+>/,"");
  in_comment=sub(/<!--+.*/,"");
  print}'

または:

xmlstarlet ed -d '//comment()' file.xml
12
Frantique