If else condition doesn't seem to evaluate rig

2019-03-01 07:18发布

here is my input xml

 <depositAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>12345678</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode/>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>99999999</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode>PDM</packageCode>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>12345678</number>
       <status>N</status>
       <productCode>KDB</productCode>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>85677833</number>
       <status>N</status>
       <productCode>KDB</productCode>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
 </depositAccount>

Output should be as below

 <depositAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>12345678</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode/>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>99999999</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode>PDM</packageCode>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>       
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>85677833</number>
       <status>N</status>
       <productCode>KDB</productCode>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
 </depositAccount>

I have the below code

<xsl:for-each-group select="$depositAccount/EligibleDepAccount[status = 'Y']" group-by="number">
    <xsl:if test="count(current-group()) = 2 and $depositAccount/EligibleDepAccount[status = 'Y']">
        <xsl:copy-of select="."/>               
    </xsl:if>
    <xsl:if test="not(current-group()[2])">
        <xsl:copy-of select="."/>               
    </xsl:if>   
</xsl:for-each-group>   

just doesn't seem to work, output coming back is below which is wrong, what is wrong with the above the xslt code ? I think there are 2 if conditions which is passing though, how can i break if the first test condition is met. please help

<depositAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>12345678</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode/>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>        
</depositAccount>

标签: xslt
2条回答
手持菜刀,她持情操
2楼-- · 2019-03-01 07:36

If you want to group by number independent of the status but then want to output the first item in each group with status = 'Y' or if there are no then the first item you can achieve that as follows:

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="depositAccount">
  <xsl:copy>
    <xsl:for-each-group select="EligibleDepAccount" group-by="number">
      <xsl:copy-of select="(current-group()[status = 'Y'], .)[1]"/>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Given the input

<depositAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>12345678</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode/>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>99999999</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode>PDM</packageCode>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>12345678</number>
       <status>N</status>
       <productCode>KDB</productCode>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
    <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>85677833</number>
       <status>N</status>
       <productCode>KDB</productCode>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
 </depositAccount>

we then get the output

<depositAccount>
   <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>12345678</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode/>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
   <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>99999999</number>
       <status>Y</status>
       <relationship>F-N</relationship>
       <productCode>ICK</productCode>
       <packageCode>PDM</packageCode>
       <primaryIndicator>N</primaryIndicator>
       <customer1DrivBen/>
       <customer2Relationship>O-N</customer2Relationship>
       <customer2DrivBen/>
       <customer3Relationship/>
       <customer3DrivBen/>
       <customer3CifKey/>
       <tierDetail>
          <level>0</level>
          <violationCounter>0</violationCounter>
          <enrollmentDate>0</enrollmentDate>
       </tierDetail>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
   <EligibleDepAccount>
       <type>DDA</type>
       <market>050</market>
       <number>85677833</number>
       <status>N</status>
       <productCode>KDB</productCode>
       <TIEligible>false</TIEligible>
    </EligibleDepAccount>
</depositAccount>
查看更多
劳资没心,怎么记你
3楼-- · 2019-03-01 07:41

You are outputting the same thing in both branches of the conditional, which suggests you might be thinking that select="." will output the current group? Perhaps you want

<xsl:copy-of select="current-group()"/>

Within xsl:for-each-group, "." refers to the first item in the current group.

查看更多
登录 后发表回答