Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strip comments from xml file and pretty-print it

Tags:

bash

sh

xml

I have this huge xml file which contains a lot of comments.

Whats the "best way" to strip out all the comments and nicely format the xml from the linux command line?

like image 828
elcuco Avatar asked Sep 23 '09 08:09

elcuco


2 Answers

you can use tidy

$ tidy -quiet -asxml -xml -indent -wrap 1024 --hide-comments 1 tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <user username="qwerty" password="ytrewq" roles="manager-gui" />
</tomcat-users>
like image 83
alexgirao Avatar answered Oct 30 '22 02:10

alexgirao


Run your XML through an identity transform XSLT, with an empty template for comments.

All of the XML content, except for the comments, will be passed through to the output.

In order to niecely format the output, set the output @indent="yes":

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<!--Match on Attributes, Elements, text nodes, and Processing Instructions-->
<xsl:template match="@*| * | text() | processing-instruction()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

<!--Empty template prevents comments from being copied into the output -->
<xsl:template match="comment()"/>

</xsl:stylesheet>
like image 32
Mads Hansen Avatar answered Oct 30 '22 00:10

Mads Hansen