Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

xml - Incrementing and checking the counter variable in XSLT

I am having little difficulty in assigning a counter variable and incrementing it and then checking for a certain value in XSLT. Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" <xsl:variable name="empty_string"/>
    <xsl:variable name="counter" select="0"/>
<xsl:template match="/Collection">
        <xsl:for-each select="Content">
            <xsl:sort select="Html/root/Event/start_date" order="ascending"/>
 <xsl:variable name="isFutureEvent">
                        <xsl:value-of select="syscom:isFutureDate(Html/root/Event/start_date)" />
                    </xsl:variable>

                    <xsl:if test="Html/root/Event != $empty_string">
                        <xsl:if test="$isFutureEvent='true'">
                            <!-- Increment Counter -->
                            <xsl:value-of select="$counter + 1"/>
                            <!-- Test if Counter < 4 -->
                            <xsl:if test="$counter &lt; 3">
                            <div class="media">
                            <!-- Do stuff here -->
                      </div>
                            </xsl:if>  <!-- End if for counter -->  
                        </xsl:if>
                    </xsl:if>
                <!--</xsl:when>-->
            <!--</xsl:choose>-->
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

But it doesnt seem to increment my counter and not exiting when the counter hits 3. Any help on this ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

'Variables' in XSL are actually constants - you cannot change their value. This:

<xsl:value-of select="$counter + 1"/> 

will just output the value of $counter+1

To do loops you have to use recursion - e.g.:

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

  <xsl:template name="loop">
    <xsl:param name="i"/>
    <xsl:param name="limit"/>
    <xsl:if test="$i &lt;= $limit">
      <div>
        <xsl:value-of select="$i"/>
      </div>
      <xsl:call-template name="loop">
        <xsl:with-param name="i" select="$i+1"/>
        <xsl:with-param name="limit" select="$limit"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/">
    <html>
      <body>
        <xsl:call-template name="loop">
          <xsl:with-param name="i" select="0"/>
          <xsl:with-param name="limit" select="10"/>
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

altough it is better to try to avoid loops - in most cases the XSL can be written to avoid it, but I don't understand enough of what are you trying to achieve to give you the complete solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...