0

I am new on this and I would like some help. I have a XML file and I need to edite you content when some cretirias are met.

Currently I'm using the sed Unix commando, but this only exclude the entire line, but I need to exclude the entire block

What I have

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
  <types>
    <name>ApexClass</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
  <types>
    <name>EmailTemplate</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
  <types>
    <name>CustomObject</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
</Package>

What I need to solve. When I for a key for example "ApexClass" I have to delete from the tag "" to "" related to this na "ApexClass"

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
  <types>
    <name>EmailTemplate</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
  <types>
    <name>CustomObject</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
</Package>

This is my currently code.

job1:
  stage: test
  script:
    - cleanItems="$(cat config/destructiveClean.list | tr -d '\r' | tr '\n' ' ')"
    - >
      if [ ! -z "$cleanItems" ]; then
          echo "=== Additional cleaning ==="          
          for cleanItem in ${cleanItems}; do
              metadaType=${cleanItem%:*}
              echo -ne "Cleaning ${metadaType} "
              sed -i "/${metadaType}/d" destructiveChanges.xml
              echo -e "\rCleaning ${metadaType} "
          done
          isDeleted="$(grep -c "<types>" destructiveChanges.xml)"
          deleted=${isDeleted%:*}
          echo -e "isDeleted ${deleted} "        
          if [ "$deleted" == 1 ]; then
              echo "=== Deleting destructiveChanges.xml file ===" 
              rm - destructiveChanges.xml
          fi          
      else 
          echo -e "Additional cleaning should be added in the config\destructiveClean.list file"
      fi

Thank you very much for you help!!

2 Answers 2

1

When dealing with structured data like XML, it's best to use a tool that actually understands the format, like xmlstarlet, which can be given an XPath expression that references the note you want to delete:

$ xmlstarlet ed -d '//_:types[_:name="ApexClass"]' input.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
  <types>
    <name>EmailTemplate</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
  <types>
    <name>CustomObject</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
</Package>
Sign up to request clarification or add additional context in comments.

3 Comments

To edit the file itself, xmlstarlet ed --inplace -d ...
Hi Shawn! Frstly, thank you so much for helping me with this. My job is running without errors, however, is not removing what I have for ApexClass variable. xmlstarlet ed --inplace -d '//_:types[_:name="${metadaType}"]' DestructiveChanges.xml The variable "metadaType" has the value "ApexClass". I added a Cat command after to ran this code and the DestructiveChanges.xml still the same. Could you help me again, please?
Hi Shawn, could you help me with this, please? Thank you so much!
0

xmlstarlet is typically not installed on a system by default, but xsltproc typically is and can be used to transform your input into the desired output using an appropriate stylesheet (XSL - XML Stylesheet Language).

$ cat demo.xsl
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:p="http://soap.sforce.com/2006/04/metadata" >
<xsl:output method='xml' encoding="UTF-8" />

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

<!-- here is where the real work occurs -->
<!-- note the use of a namespace prefix (p:) -->
<xsl:template match='//p:types[p:name="ApexClass"]' />

</xsl:stylesheet>

$ xsltproc demo.xsl input.xml
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
  
  <types>
    <name>EmailTemplate</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
  <types>
    <name>CustomObject</name>
    <members>CC_CaseScenarioMatchingJobQueueable</members>
  </types>
</Package>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.