Bulk Substitution in XML

Asked

Viewed 134 times

4

Good morning!

I have a program that check some signals, but in the code is indicated only which signal should appear, so I must apply the specific information that should be checked. But manually making these replacements takes a long time so I would like to replace in bulk.

My current code:

<comment>Check sinal P1R#</comment>

Replace to:

<comment>P1R#</comment>
<capltestfunction title="RUN INSPECTION" name="RunInspection">
    <caplparam type="string" name="InspName" />
</capltestfunction>
<capltestfunction title="ADD IMAGE TO REPORT" name="AddInspectionImageToReport" />
<capltestfunction title="CHECK MESSAGE" name="CheckStepResultCameraText">
    <caplparam type="string" name="StepName">TM_IN023_1</caplparam>
    <caplparam type="string" name="ExpVal">STEP</caplparam>
    <caplparam type="int" name="ContainsExpVal" />
    <caplparam type="int" name="TolerateSimilarChars">1</caplparam>
</capltestfunction>

I could not replace normally because the content is very extensive. I am learning to program yet so I need a help!

Thank you!

  • What is "Check sign" there goes a +, -? you want to replace everything this and top by the bottom, that?

  • That’s right William lautert ! I would like to replace everything above with the below. This Check signal P1R# is just a comment described which should be checked that in the case would be the signal "P1R#". At the bottom are the Steps that I use for this specific test. Thank you for your attention.

1 answer

2


Use regular expressions to do Parsing and transformation of XML it’s not a good idea. Your problem demands both things. The tool to be used must first find tags comment within the original document and then replace them with the desired fragment (with dynamic variations depending on the content of the tag original).

For this type of transformation it is worth using a tool capable of handling the idiosyncrasies of XML. Good and old XSLT is usually enough for that sort of thing.

Here’s a stylesheet that solves the problem of the question:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes" method="xml" />
<xsl:strip-space elements="*"/>
   <!-- Identity transform -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="comment">
       <comment><xsl:value-of select="substring-after(text(), 'Check sinal ')" /></comment>
       <capltestfunction title="RUN INSPECTION" name="RunInspection">
           <caplparam type="string" name="InspName" />
       </capltestfunction>
       <capltestfunction title="ADD IMAGE TO REPORT" name="AddInspectionImageToReport" />
       <capltestfunction title="CHECK MESSAGE" name="CheckStepResultCameraText">
           <caplparam type="string" name="StepName">TM_IN023_1</caplparam>
           <caplparam type="string" name="ExpVal">STEP</caplparam>
           <caplparam type="int" name="ContainsExpVal" />
           <caplparam type="int" name="TolerateSimilarChars">1</caplparam>
       </capltestfunction>
   </xsl:template>
</xsl:stylesheet>

See working on XSL Transform


The first four lines declare a stylesheet whose output is a well formatted XML, indented and without unnecessary spaces between the elements.

The first template is the "template identity" that simply copies the original content to the output.

The second template - more specific than the first and therefore with higher priority - replaces tags comment by new fragments composed of:

  1. Other tag comment containing the piece of text after the string Check Signal in tag comment original:

    <comment><xsl:value-of select="substring-after(text(), 'Check sinal ')" /></comment>
    
  2. As tags fixed-type capltestfunction and caplparam that you wish to include.

From this example you can build more complex transformations without relying on regular expressions that become more and more fragile and inefficient as the complexity of the problem increases.

  • Thanks for the strength Anthony Accioly! I’ll try to do here!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.