-2

I have an XML file which contents a commented section. My requirement is to uncomment it using a shell script. Any help would be highly appreciated.

<!--<transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.from">xx</parameter>
        <parameter name="mail.smtp.user">xx</parameter>
        <parameter name="mail.smtp.password">xx</parameter>
        <parameter name="mail.smtp.host">smtp.gmail.com</parameter>
        <parameter name="mail.smtp.port">587</parameter>
        <parameter name="mail.smtp.starttls.enable">true</parameter>
        <parameter name="mail.smtp.auth">true</parameter>
    </transportSender>-->
Pierre François
  • 5,850
  • 1
  • 17
  • 38
cHaNkx
  • 15
  • 2
  • Check answer to this question, it should help https://stackoverflow.com/questions/525592/find-and-replace-inside-a-text-file-from-a-bash-command – anupkholgade Feb 20 '18 at 12:30
  • Do you want to uncomment all comments or only a specific section. In this case, please give a criterion to know which section should be uncommented. – Pierre François Feb 20 '18 at 13:03
  • This is a useful question. Some people on SO propose to solve it with **sed** or **perl**, but that is not what **xml** is made for. The solution will involve xml tools like **xsltproc** or similar. – Pierre François Feb 20 '18 at 13:06
  • This is not a code writing service. What did you try so far? Post your code! What happened when you ran it? What did you expect to happen instead? What specifically are you having problems with? https://stackoverflow.com/help/mcve – Robert Feb 20 '18 at 16:48

2 Answers2

2

Using xsltproc, you will need this file my.xsl to process your xml input:

<?xml version="1.0" encoding="utf-8"?>

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

  <xsl:output method="xml" indent="yes" encoding="utf-8"/>

  <xsl:template match="@*|node()"><!-- identity transformation -->
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="comment()"><!-- comments transformation -->
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

Assuming your input file my.xml looks like this:

<?xml version="1.0" ?>
<my-tag>
  <!--<transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.from">xx/parameter>
        <parameter name="mail.smtp.user">xx</parameter>
        <parameter name="mail.smtp.password">xx</parameter>
        <parameter name="mail.smtp.host">smtp.gmail.com</parameter>
        <parameter name="mail.smtp.port">587</parameter>
        <parameter name="mail.smtp.starttls.enable">true</parameter>
        <parameter name="mail.smtp.auth">true</parameter>
    </transportSender>-->
</my-tag>

Now, you can run next command:

xsltproc my.xsl my.xml | sed -e 's/&lt;/</g' -e 's/&gt;/>/g'

The sed transformation was necessary to restore the '<' and '>' chars in the output.

This gives you following output:

<?xml version="1.0" encoding="utf-8"?>
<my-tag>
  <transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">test
        <parameter name="mail.smtp.from">xx/parameter>
        <parameter name="mail.smtp.user">xx</parameter>
        <parameter name="mail.smtp.password">xx</parameter>
        <parameter name="mail.smtp.host">smtp.gmail.com</parameter>
        <parameter name="mail.smtp.port">587</parameter>
        <parameter name="mail.smtp.starttls.enable">true</parameter>
        <parameter name="mail.smtp.auth">true</parameter>
    </transportSender>
</my-tag>

If you don't want to uncomment all the comments, you can write a more specific rule to select only the comment nodes you want to uncomment. If you don't know how, just ask for it.

Pierre François
  • 5,850
  • 1
  • 17
  • 38
0

Adding disable-output-escaping="yes" needed for special char escaping in output file...

<?xml version="1.0" encoding="utf-8"?>

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

  <xsl:output method="xml" indent="yes" encoding="utf-8"/>

  <xsl:template match="@*|node()"><!-- identity transformation -->
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="comment()"><!-- comments transformation -->
    <xsl:value-of select="." disable-output-escaping="yes" />
  </xsl:template>

</xsl:stylesheet>
ahmettolga
  • 996
  • 7
  • 16