Search code examples
xmlxsltxslt-2.0

Need to remove the plan value for multiple condition


I need to remove the plan options for the same multiple value occurrence

test.xml

<p base="OptionDesc"></p>

Input XML value having:

<PlanMaps>
    <PlanMap>
        <PlanCode>10049</PlanCode>
        <Strategy>F</Strategy>
        <OptionDesc>Option 2424809</OptionDesc>
    </PlanMap>
    <PlanMap>
        <PlanCode>10049</PlanCode>
        <Strategy>F</Strategy>
        <OptionDesc>Option 2424796</OptionDesc>
    </PlanMap>
    <PlanMap>
        <PlanCode>10049</PlanCode>
        <Strategy>F</Strategy>
        <OptionDesc>Option 2414596</OptionDesc>
    </PlanMap>
    <PlanMap>
        <PlanCode>30019</PlanCode>
        <Strategy>V</Strategy>
        <OptionDesc>Option 2414600</OptionDesc>
    </PlanMap>
  </PlanMaps>

XSL I have tried:

<xsl:template match="PlanMaps">
      <xsl:apply-templates select="document('../../../../test.xml')"/>
   </xsl:template>  
   
<xsl:template match="p[@base='OptionDesc'][parent::entry/@outputclass='plans']">
    <p base="OptionDesc">
        <xsl:text>(</xsl:text>
        <xsl:value-of select="PlanMaps/PlanMap/OptionDesc"/>    
        <xsl:text>)</xsl:text>
    </p>
</xsl:template>

Expectation of result when the Strategy value is "F" and when PlanCode value (10049) is same for multiple occurrence(two or more), then result should be empty like below

<p base="OptionDesc"></p>
<p base="OptionDesc">(Option 2414600)</p>

Here I need the XSL without mentioning the value of plancode in XSL.


Solution

  • It seems you just need to process the primary input:

      <xsl:template match="PlanMaps">
          <xsl:for-each-group select="PlanMap" group-by="concat(PlanCode, '|',  Strategy = 'F')">
              <p base="OptionDesc">
                  <xsl:value-of select="concat('(', OptionDesc, ')')[not(current-group()[2] and current-group()/Strategy = 'F')]"/>
              </p>
          </xsl:for-each-group>
      </xsl:template>
    

    https://xsltfiddle.liberty-development.net/bEJbVrN/1