Search code examples
xsltxslt-1.0xslt-grouping

How to append string values inside if condition


I am new to XSL.I have an XML as below, If CoverageCode equals -'HomeCoverage' then I have to verify for the next 3 elements of 'roofRestrictionEndt','sidingRestrictionEndt'and 'paintRestrictionEndt' . If 'roofRestrictionEndt' exits and its value is 'Y' then I need to print 'Roof' under the 'results' tag, If 'sidingRestrictionEndt' exists and its value is 'Y' then I need to print 'siding' in case if it exists along with the above one then I need to print 'Roof; siding'. If 'paintRestrictionEndt' exists and its value is 'Y' along with the other 2 elements then I need to print 'Roof; siding; paint'. I tried by declaring variables and wrote If conditions and tried to append values accordingly inside IF condition, but I came to know the declared variables are immutable. In java, we can achieve this by using StringBuffer. Is there any way to achieve this in XSL? Below is XML.

<locationCoverage ID="3">

<coverageCode >HomeCoverage</coverageCode>
<roofRestrictionEndt >Y</roofRestrictionEndt>
      <sidingRestrictionEndt>Y</sidingRestrictionEndt>
      <paintRestrictionEndt >Y</paintRestrictionEndt>
<locationCoverage>

Results should look like as below

<results>
      <result>Roof;siding;paint</result>
      
      </results>

If I have below input XML

<locationCoverage ID="3">
<coverageCode >HomeCoverage</coverageCode>
<roofRestrictionEndt >Y</roofRestrictionEndt>
 <paintRestrictionEndt >Y</paintRestrictionEndt>
</locationCoverage>

For the above XML results should look like as below

<results>
      <result>Roof;paint</result>
      
      </results>

Appreciate it If anyone helps me with this. Thanks in advance.


Solution

  • If I understand this correctly (which is not at all certain), you want to do something like:

    <xsl:template match="locationCoverage[coverageCode='HomeCoverage']">
        <xsl:variable name="test-results">
            <xsl:if test="roofRestrictionEndt='Y'">Roof </xsl:if>
            <xsl:if test="sidingRestrictionEndt='Y'">siding </xsl:if>
            <xsl:if test="paintRestrictionEndt='Y'">paint</xsl:if>
        </xsl:variable>
        <result>
            <xsl:value-of select="translate(normalize-space($test-results), ' ', ';')"/>
        </result>
    </xsl:template>