1

I am trying to do a shell script with 2 parameters.

xmlcomment -c cholo

or

xmlcomment -u cholo

Given the following xml file,

<?xml version="1.0"?>
<guyList>
<guy>
    <name>paco</name>
    <fullname>Paco Tilla</fullname>
    <theme>paco</theme>
  </guy>
  <guy>
    <name>cholo</name>
    <fullname>Cholo Cote</fullname>
    <theme>cholocote</theme>
  </guy>
  <guy>
    <name>temo</name>
    <fullname>Temo Lesto</fullname>
    <theme>temol</theme>
  </guy>
</guyList>

The first comments out the block "<guy>" that contains a "<name>paco</name>" , resulting in the following file change.

<?xml version="1.0"?>
<guyList>
<guy>
    <name>paco</name>
    <fullname>Paco Tilla</fullname>
    <theme>paco</theme>
  </guy>
<!--
  <guy>
    <name>cholo</name>
    <fullname>Cholo Cote</fullname>
    <theme>cholocote</theme>
  </guy>
-->
  <guy>
    <name>temo</name>
    <fullname>Temo Lesto</fullname>
    <theme>temol</theme>
  </guy>
</guyList>

The second example just uncomment that same block (if commented) like presented in the first XML.

Any Idea on a good approach to do this? sed ? some other XML editor?

Thanks

0

You can use an XSLT processor for this, like xsltproc:

<xsl:template match="guy[name='cholo']">
  <xsl:comment>
    <xsl:apply-templates select="@*|node()" />
  </xsl:comment>
</xsl:template>

This approach does not fully reproduce the original XML structure, but it comments out the appropriate guy section.

The output is:

<guyList>
    <guy>
        <name>paco</name>
        <fullname>Paco Tilla</fullname>
        <theme>paco</theme>
    </guy>
    <!--
      cholo
      Cholo Cote
      cholocote
    -->
    <guy>
        <name>temo</name>
        <fullname>Temo Lesto</fullname>
        <theme>temol</theme>
    </guy>
</guyList>
  • Thanks, but this option will be kind of destructive for the file to go back in an uncomment operation as I stated in my question. – FedeKrum Dec 31 '18 at 16:58
0

Can be done with sed (maybe not the best approach). The scripts are the same, just replacing line addresses and what substitute in each case.

sed '/<guy>/ {
    :a
    N
    /<\/guy>/ {
            /cholo/ {
                    s/^/<!--\n/
                    s/$/\n-->/
            }
    p
    d
    }
    ba
}' file-without-comment
<?xml version="1.0"?>
<guyList>
<guy>
    <name>paco</name>
    <fullname>Paco Tilla</fullname>
    <theme>paco</theme>
  </guy>
<!--
  <guy>
    <name>cholo</name>
    <fullname>Cholo Cote</fullname>
    <theme>cholocote</theme>
  </guy>
-->
  <guy>
    <name>temo</name>
    <fullname>Temo Lesto</fullname>
    <theme>temol</theme>
  </guy>
</guyList>

sed '/<!--/ {
    :a
    N
    /-->/ {
            /cholo/ {
                    s/<!--\n//
                    s/\n-->//
            }
    p
    d
    }
    ba
}' file-with-comment                                          
<?xml version="1.0"?>
<guyList>
<guy>
    <name>paco</name>
    <fullname>Paco Tilla</fullname>
    <theme>paco</theme>
  </guy>
  <guy>
    <name>cholo</name>
    <fullname>Cholo Cote</fullname>
    <theme>cholocote</theme>
  </guy>
  <guy>
    <name>temo</name>
    <fullname>Temo Lesto</fullname>
    <theme>temol</theme>
  </guy>
</guyList>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.