Skip to main content
4 of 6
edited body
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

Using xmlstarlet with an XSL transformation in transform.xsl:

<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/|node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="/Context/Valve"> <xsl:text disable-output-escaping="yes">&lt;!-- </xsl:text> <xsl:copy-of select="."/> <xsl:text disable-output-escaping="yes"> --&gt;</xsl:text> </xsl:template> </xsl:stylesheet> 

The first template is just an identity transformation, while the second template inserts <!-- and --> around the /Context/Valve node.

Applying it:

$ xmlstarlet tr transform.xsl file.xml <?xml version="1.0"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <Context antiResourceLocking="false" privileged="true"> <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1"/> --> <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> </Context> 

This would handle commenting out the node whatever the formatting of it happens to be.

If it's a static file that won't change, then just insert the comment markup on the correct lines:

sed '17s/^/<!-- /; 18s/$/ -->/' file.xml 
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k