Counting the number of hits for each

I have the code below. I would like each one to stop after if-sentenses came back and executed a code block 4 times. Since I have no idea when the code block was executed 4 times, I cannot use position (), which was my first idea.

Any help would be greatly appreciated.

<xsl:for-each select="$itm//item[@template='news item']">
    <xsl:sort select="sc:fld('start date',.)" data-type="text" order="descending"/>
    <xsl:if test="sc:formatdate($date,'yyyyMMddHHmm') &gt; sc:formatdate(sc:fld('start date',.),'yyyyMMddHHmm')">
        <xsl:if test="sc:formatdate($date,'yyyyMMddHHmm') &lt; sc:formatdate(sc:fld('end date',.),'yyyyMMddHHmm')">
    <!--EXECUTE A CODE BLOCK-->
        </xsl:if>
    </xsl:if>
</xsl:for-each>
+3
source share
4 answers
<!-- convenience variables -->
<xsl:variable name="dtFormat" select="'yyyyMMddHHmm'" />
<xsl:variable name="theDate" select="sc:formatdate($date, $dtFormat)" />

<!-- don't loop over nodes testing a condition on each one separately, 
     just select the interesting ones directly -->
<xsl:variable name="MatchingFields" select="
  $itm//item[@template='news item'][
    sc:formatdate(sc:fld('start date', .), $dtFormat) &lt; $theDate
    and
    sc:formatdate(sc:fld('end date', .), $dtFormat) &gt; $theDate
  ]
" />

<!-- now do something with the nodes -->
<xsl:for-each select="$MatchingFields">
  <xsl:sort select="sc:fld('start date',.)" data-type="text" order="descending"/>
  <!--EXECUTE A CODE BLOCK-->
</xsl:for-each>

<!-- you can also count them (this is your "hit counter") -->
<xsl:value-of select="count($MatchingFields)" />
+1
source

No, because xslt is not a procedural language, the operator says that it works in parallel, receiving all the data at the same time, and then displaying it. therefore, he does not know about iteration.

0
source

, "" for xlst (. ). -:

  • select, - , .
  • ( , -param). , . , 4, (.. ).

      

      

    <!-- exit condition -->
    <xsl:if test="$executed &lt; 4">
        <!-- check conditions and select next item -->
        <xsl:choose>
            <!-- condition for items where the code should be executed -->
            <xsl:when test="test whether code should be executed">
                <!-- execute your code here -->
                <xsl:apply-templates select="select next item" mode="recursive">
                    <xsl:with-param name="executed" select="$executed + 1"/>
                </xsl:apply-templates>
            </xsl:when>
            <!-- condition for items where the code should not be executed -->
            <xsl:otherwise>
                <xsl:apply-templates select="select next item" mode="recursive">
                    <xsl:with-param name="executed" select="$executed"/>
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
    

0

select for-each, :

<xsl:for-each select="$itm//item[@template='news item'][sc:formatdate($date,'yyyyMMddHHmm') &gt; sc:formatdate(sc:fld('start date',.),'yyyyMMddHHmm')][sc:formatdate($date,'yyyyMMddHHmm') &lt; sc:formatdate(sc:fld('end date',.),'yyyyMMddHHmm')]">
    <xsl:sort select="sc:fld('start date',.)" data-type="text" order="descending"/>
    <xsl:if test="position() &lt; 5">
      <!-- output something -->
    </xsl:if>
</xsl:for-each>
0

Source: https://habr.com/ru/post/1733382/


All Articles