简体   繁体   中英

How to add conditions to xsl for-each statements

I'm trying to filter out any amount below $1,000,000. Here is the code:

<xsl:for-each select="row">
   <tr>
      <xsl:for-each select="cell">
         <xsl:if test="position()=5">
            <td width="140" style="vertical-align: top; padding: 3px;" valign="top">
                 <xsl:choose>
                  <xsl:when test="@href">
                     <a href="">
                        <xsl:attribute name="href">
                           <xsl:value-of select="@href" />
                        </xsl:attribute>
                        <xsl:value-of select="." />
                     </a>
                  </xsl:when>
                  <xsl:otherwise>
                     <xsl:value-of select="." />
                  </xsl:otherwise>
               </xsl:choose>
            </td>
         </xsl:if>
         <xsl:if test="position()=8">
            <td class="categorydetail-last-column" style="text-align: right; vertical-align: top; padding: 3px;" align="right" valign="top">
               <xsl:value-of select="." /><!-- THIS OUTPUTS NUMBERS. WANT TO FILTER NUMBERS BELOW 1000000 -->
            </td>
         </xsl:if>
      </xsl:for-each>
   </tr>
</xsl:for-each>

It outputs dollar amount but it needs to output if it's bigger than one million. I tried creating variables and wrapping the whole code in IF statement but it didn't work. What am I doing wrong? Do I have to add condition to the ROW?

As I understood, you want to process only rows in which cell No 8 contains value less than a million.

So use a template with the match condition specifying just this filtering criterion:

<xsl:template match="row[cell[8] &lt; 1000000]">
  ...
</xsl:template>

If your XSLT script contains also an identity template , you must somehow "block" other rows from proessing. You can do it with a "more general", empty template, for all rows:

<xsl:template match="row"/>

Actually, it would be applied to all "other" rows (to which the former template does not apply).

Another solution (more close to the script you posted):

Change the "external" loop to:

<xsl:for-each select="row[cell[8] &lt; 1000000]">

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