2

I want to make a Counter value getting incremented based on a condition. But i was not able to increment a value or add 1 to a global variable. I was using the following code

<xsl:variable name="count">0</xsl:variable>
<xsl:variable name="incre">1</xsl:variable>    

<xsl:template match="/">
  <xsl:if test="$video1 != ''">
    <xsl:variable name="count" select="number($count)+number($incrementor)"/>
    <!-- display video1 -->
  </xsl:if>
  <xsl:if test="$video2 != ''">
    <xsl:variable name="count" select="number($count)+number($incrementor)"/>
    <!-- display video2 -->
  </xsl:if>
  <xsl:if test="$video3 != ''">
    <xsl:if test="$count<2">
      <xsl:variable name="count" select="number($count)+number($incrementor)"/>
      <!-- display video3 -->
    </xs:if>
  </xsl:if>
  <xsl:if test="$video4 != ''">
    <xsl:if test="$count<2">
      <xsl:variable name="count" select="number($count)+number($incrementor)"/>
      <!-- display video4 -->
    </xs:if>
  </xsl:if>
</template>

How to increment the value of count or add 1 to it. Thanks in advance.

3
  • 1
    There is no way to change the value of an variable in xslt after first initialization. Perhaps you should explain what the goal. Something like display the first two videos where the value is not empty? Commented May 30, 2013 at 15:11
  • @hr_117 Yes i want to show 2 non empty videos out of 6 Commented May 30, 2013 at 15:59
  • Can you post an XML sample? xsl:number may work better for you than count(). Commented May 30, 2013 at 19:29

1 Answer 1

2

Your problem arises from thinking like a procedural programmer when trying to use a functional language. It's a bad mix; tools work a lot better when you work with them and not against them.

Don't try to walk through the document and increment variables to keep count of things. Describe what you want in declarative terms.

Here, you want the first two non-empty <video> elements in the document to display, and you want the others (all the empty <video> elements and the third and later non-empty <video> elements) to be passed over in silence. (I'm making this up, of course: since you don't show your XML, I don't actually know what you really want, in XML terms.)

So you want a template to suppress empty <video> elements:

<xsl:template match="video[not(node())]"/>

And you want a template to suppress any <video> element preceded in the document by two or more non-empty <video> elements:

<xsl:template 
  match="video[count(preceding::video
                     [* or normalized-space(.)]
                    ) 
               > 1]"/>

And you want a template to display the first two non-empty <video> elements:

<xsl:template 
  match="video[* or normalized-space(.)]
              [count(preceding::video
                     [* or normalized-space(.)]
                    ) 
               &lt; 2]">

  <xsl:message>Displaying video ... </xsl:message>
  <!--* ... whatever else is needed to display the video ... *-->

<xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.