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>
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:
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>
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.
What is "Check sign" there goes a +, -? you want to replace everything this and top by the bottom, that?
– Guilherme Lautert
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.
– MISFITS