Home > Back-end >  Remove block from XML file
Remove block from XML file

Time:10-05

I want to remove the following block ('valve');

                    <Valve className="org.apache.catalina.valves.AccessLogValve"
                           directory="logs"
                           maxDays="30"
                           pattern="%t %{X-AUSERNAME}o %I %h %r %s %Dms %b %{Referer}i %{User-Agent}i"
                           prefix="conf_access_log"
                           requestAttributesEnabled="true"
                           rotatable="true"
                           suffix=".log"
                    />

From the following XML file;

<Server port="8000" shutdown="SHUTDOWN" debug="0">
    <Service name="Tomcat-Standalone">
        <!--
         ==============================================================================================================
         DEFAULT - Direct connector with no proxy, for unproxied HTTP access to Confluence.

         If using a http/https proxy, comment out this connector.
         ==============================================================================================================
        -->
        <Connector port="8090" connectionTimeout="20000" redirectPort="8443"
                   maxThreads="48" minSpareThreads="10"
                   enableLookups="false" acceptCount="10" debug="0" URIEncoding="UTF-8"
                   protocol="org.apache.coyote.http11.Http11NioProtocol"/>
        <!--
         ==============================================================================================================
         HTTP - Proxying Confluence via Apache or Nginx over HTTP

         If you're proxying traffic to Confluence over HTTP, uncomment the connector below and comment out the others.
         Make sure you provide the right information for proxyName and proxyPort.

         For more information see:
            Apache - https://confluence.atlassian.com/x/4xQLM
            nginx  - https://confluence.atlassian.com/x/TgSvEg

         ==============================================================================================================
        -->

        <!--
        <Connector port="8090" connectionTimeout="20000" redirectPort="8443"
                   maxThreads="48" minSpareThreads="10"
                   enableLookups="false" acceptCount="10" debug="0" URIEncoding="UTF-8"
                   protocol="org.apache.coyote.http11.Http11NioProtocol"
                   scheme="http" proxyName="<subdomain>.<domain>.com" proxyPort="80"/>
        -->

        <!--
         ==============================================================================================================
         HTTPS - Direct connector with no proxy, for unproxied HTTPS access to Confluence.

         For more info see https://confluence.atlassian.com/x/s3UC
         ==============================================================================================================
        -->

        <!--
        <Connector port="8443" maxHttpHeaderSize="8192"
                   maxThreads="150" minSpareThreads="25"
                   protocol="org.apache.coyote.http11.Http11Nio2Protocol"
                   enableLookups="false" disableUploadTimeout="true"
                   acceptCount="100" scheme="https" secure="true"
                   clientAuth="false" sslProtocol="TLSv1.2" sslEnabledProtocols="TLSv1.2" SSLEnabled="true"
                   URIEncoding="UTF-8" keystorePass="<MY_CERTIFICATE_PASSWORD>"/>
        -->

        <!--
         ==============================================================================================================
         HTTPS - Proxying Confluence via Apache or Nginx over HTTPS

         If you're proxying traffic to Confluence over HTTPS, uncomment the connector below and comment out the others.
         Make sure you provide the right information for proxyName and proxyPort.

         For more information see:
            Apache - https://confluence.atlassian.com/x/PTT3MQ
            nginx  - https://confluence.atlassian.com/x/cNIvMw
         ==============================================================================================================
        -->

        <!--
        <Connector port="8090" connectionTimeout="20000" redirectPort="8443"
                   maxThreads="48" minSpareThreads="10"
                   enableLookups="false" acceptCount="10" debug="0" URIEncoding="UTF-8"
                   protocol="org.apache.coyote.http11.Http11NioProtocol"
                   scheme="https" secure="true" proxyName="<subdomain>.<domain>.com" proxyPort="443"/>
        -->

        <Engine name="Standalone" defaultHost="localhost" debug="0">
            <Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="false" startStopThreads="4">
                <Context path="" docBase="../confluence" debug="0" reloadable="false" useHttpOnly="true">
                    <!-- Logging configuration for Confluence is specified in confluence/WEB-INF/classes/log4j.properties -->
                    <Manager pathname=""/>
                    <Valve className="org.apache.catalina.valves.StuckThreadDetectionValve" threshold="60"/>

                    <!-- http://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Access_Log_Valve -->
                    <Valve className="org.apache.catalina.valves.AccessLogValve"
                           directory="logs"
                           maxDays="30"
                           pattern="%t %{X-AUSERNAME}o %I %h %r %s %Dms %b %{Referer}i %{User-Agent}i"
                           prefix="conf_access_log"
                           requestAttributesEnabled="true"
                           rotatable="true"
                           suffix=".log"
                    />

                    <!-- http://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Remote_IP_Valve -->
                    <Valve className="org.apache.catalina.valves.RemoteIpValve" />
                </Context>

                <Context path="${confluence.context.path}/synchrony-proxy" docBase="../synchrony-proxy" debug="0"
                         reloadable="false" useHttpOnly="true">
                    <Valve className="org.apache.catalina.valves.StuckThreadDetectionValve" threshold="60"/>
                </Context>
            </Host>
        </Engine>
    </Service>
</Server>

But my understanding of xmlstarlet is apparently too weak :)

What i tried;

     xmlstarlet ed -d "//valve[className='org.apache.catalina.valves.AccessLogValve']" server.xml
     xmlstarlet ed -d "//service[className='org.apache.catalina.valves.AccessLogValve']" server.xml
     xmlstarlet ed -d "//service[Valve className='org.apache.catalina.valves.AccessLogValve']" server.xml
     xmlstarlet ed -d "//Server/Service/Engine/Host/Context[Valve className='org.apache.catalina.valves.AccessLogValve']" server.xml
     xmlstarlet ed -d "//Server/Service/Engine/Host/Context[className='org.apache.catalina.valves.AccessLogValve']" server.xml
     xmlstarlet ed -d "//Server/Service/Engine/Host/Context/Valve[className='org.apache.catalina.valves.AccessLogValve']" server.xml

What am i doing wrong?

Thanks in advance!

EDIT: What does xsltproc do exactly?

I can see the differences when i diff the 2 files, e.g.

<                     <Valve className="org.apache.catalina.valves.AccessLogValve"
<                            directory="logs"
<                            maxDays="30"
<                            pattern="%t %{X-AUSERNAME}o %I %h %r %s %Dms %b %{Referer}i %{User-Agent}i"
<                            prefix="conf_access_log"
<                            requestAttributesEnabled="true"
<                            rotatable="true"
<                            suffix=".log"
<                     />
---
>                     <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" maxDays="30" pattern="%t %{X-AUSERNAME}o %I %h %r %s %Dms %b %{Referer}i %{User-Agent}i" prefix="conf_access_log" requestAttributesEnabled="true" rotatable="true" suffix=".log"/>
94c84

But perhaps my question wasn't clear, i'd like to remove the block called <Valve className="org.apache.catalina.valves.AccessLogValve"> entirely.

CodePudding user response:

Use xsltproc with this xsl transform :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Valve"/>
</xsl:stylesheet>

This transform is based on identity transform with one template which will remove all nodes "Valve".

For more restrictive :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Valve[@className=‘org.apache.catalina.valves.AccessLogValve’]"/>
</xsl:stylesheet>

This one will remove node Valve only if it’s attribute className is the one specified.

Usage :

xsltproc myTransfo.xsl myXmlFile.xml > newXmlfile.xml

xsltproc is a xsl processor : it takes an xmlfile in input and apply all the xsl transformation which are describe in a xsl file. In your case, it will copy the entire xml file without node with name Valve.

Xsltproc will not reformat xml output as It was in input. To get more pretty output you can look at xmllint command.

  •  Tags:  
  • xml
  • Related