简体   繁体   中英

how to count using for each in xsl

please see below , sample xml

<OuttypList>
   <Outtyp>
         <A>
            <P>0</P>
            <T>NoShare</T>
         </A>
   </Outtyp>
    <Outtyp>
        <Outtypform>Bank</Outtypform>
        <A>
            <A>
                <P>1000</P>
                <T>Share</T>
            </A>
            <S>100</S>
        </A>
        <B>         
            <C>3015</C>
            <D>James</D>
        </B>
    </Outtyp>
    <Outtyp>
    <Outtypform>Bank</Outtypform>
         <A>
            <P>10</P>
            <T>Share</T>
         </A>
   </Outtyp></OuttypList>

and I am displaying details of Outtyp and also after displaying each Outtyp I am inserting a space so that data looks good.

<xsl:for-each select="OuttypList/Outtyp">
<xsl:if test="normalize-space(Outtypform)">
--displaying details--
</xsl:if>
<xsl:choose>
        <xsl:when test="position() != last()">
              <tr>
                    <td colspan="2">
                          <span><text>&#160;</text></span>
                    </td>
              </tr>
        </xsl:when>
        <xsl:otherwise></xsl:otherwise>
</xsl:choose>

Problem : right now with this code , it creates extra space initially which I dont want, so I dont want extra space line if there is no Outtypform present in Outtyp..need to change "for each" such that it should count Outtyp only on basis if Outtypform is present under Outtyp. For example - above sample xml have 3 Outtyp node ... .but only two contains so only two node with details should be displayed and extra space after each Outtyp.

Either place the xsl:choose block inside the xsl:if block, or - preferably - change your selection to:

<xsl:for-each select="OuttypList/Outtyp[normalize-space(Outtypform)]">

Then you won't need the xsl:if at all.

Untested, because the code provided is incomplete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM