Unable to get the count of conditional results from a for-each loop outside the loop
Hi,
I'm trying to get the count of the records that satisfy my specific conditions within a for-each loop and use that count for processing outside the loop.
Could you please post a sample of the code you're trying to get working?
It should not be neccesary to do a for-each loop in xslt in order to count specific nodes since you should be able to simply do something like <xsl:value-of select="count($currentPage/Textpage)" />
Because variables in XSLT are really just constants (they're designed for stuff that may change from transformation to transformation - not changing while transforming) you cannot re-assign or change a variable once it's set. That may seem like a Catch-22 when you can't declare a variable before a for-each, and then set its value inside, to use it back out on the other side again. But the thing is - if you can get the value inside the for-each, you can also get it outside.
So if, for instance, you need to know how many pages were processed, to display some kind of counter - cache the pages in a variable before the loop (to avoid executing the XPath twice) and iterate the variable for the loop, then use the count() afterwards, e.g.:
If you're doing something like that, you can move that condition into a 'predicate' on the selection you're counting, e.g. to elaborate on my original sample:
Unable to get the count of conditional results from a for-each loop outside the loop
Hi,
I'm trying to get the count of the records that satisfy my specific conditions within a for-each loop and use that count for processing outside the loop.
How to do it?
Please let me know if anybody knows it.
Thanks in advance.
Regards,
Jaihind
Hi Jaihind and welcome to our :)
Could you please post a sample of the code you're trying to get working?
It should not be neccesary to do a for-each loop in xslt in order to count specific nodes since you should be able to simply do something like <xsl:value-of select="count($currentPage/Textpage)" />
Looking forward to hearing more from you.
/Jan
Hi Jaihind,
Because variables in XSLT are really just constants (they're designed for stuff that may change from transformation to transformation - not changing while transforming) you cannot re-assign or change a variable once it's set. That may seem like a Catch-22 when you can't declare a variable before a for-each, and then set its value inside, to use it back out on the other side again. But the thing is - if you can get the value inside the for-each, you can also get it outside.
So if, for instance, you need to know how many pages were processed, to display some kind of counter - cache the pages in a variable before the loop (to avoid executing the XPath twice) and iterate the variable for the loop, then use the count() afterwards, e.g.:
/Chriztian
Hi,
I'm trying to apply the logic wherein I am trying to reduce the count on the basis of particular condition and use it further.
Code is like this -
<xsl:variable name="totalCount"><xsl:numbervalue="count(//node/dubnode)" />xsl:variable>
< xsl:for-eachselect="//node/subnode">
<xsl:iftest="condition 1 and condition ''">
--reduce the count by one
<xsl:if>
<xsl:for-each>
I'm trying to use that modified count after the for loop.
As we cannot reassign the value back to the variable, I'm looking for an alternative way.
Regards,
Jaihind
Hi Jaihind,
If you're doing something like that, you can move that condition into a 'predicate' on the selection you're counting, e.g. to elaborate on my original sample:
/Chriztian
Thank you very much Chriztian..
I was stuck because of this issue for last two days.
This thing worked.
Thanks once again.
Regards,
Jaihind
Great!
Henceforth let it be known that pancakes solves all our problems :-)
/Chriztian
lol :)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.