I want to count the times xslt loops into a for-each. Any idea?
pseudocode:
<xsl:for-each select="child::*"> <xsl:variable name="i" select="$i + 1"/> <!-- add 1 each time --> </xsl:for-each> <xsl:value-of select="$i"/> <!-- show the result -->
That's not easy with XSLT, as variables have a fixed value.
One way is to use a recursively called named template, to which you pass a parameter that, in the recursion, gets the original value + 1).
Usually, though, recursively called named templates are seldom the best option. Can you tell use a bit more in detail what you'd like to achieve with your FOR loop?
I have an xslt that looks for child nodes of type X in a tree, I want it to count each child node and then show the value but, I can't store the count of each node in the variable.Here is an example:
Tree:
Parent - Parent1 - Child1 - Child2
- Parent2 - Child1 - Child 2
What code should do:
Count child nodes and show the number of child nodes. ( In this case the value shoud be 4 )
Status:
The function shows the childs perfectly but, as I said before, I need to store the nodes number in a variable to show it later.
Thanks for your answer but, that is not exactly what I am looking for. I want to show a fixed number of nodes, that's why I want to know how many nodes I have shown. I think I can store all childs information within a variable and then show only the number of nodes that I want. Is this correct?
XSLT Iterator
Good morning,
I want to count the times xslt loops into a for-each. Any idea?
pseudocode:
<xsl:for-each select="child::*">
<xsl:variable name="i" select="$i + 1"/> <!-- add 1 each time -->
</xsl:for-each>
<xsl:value-of select="$i"/> <!-- show the result -->
Thanks beforehand.
Sincere regards,
Eduardo Macho
That's not easy with XSLT, as variables have a fixed value.
One way is to use a recursively called named template, to which you pass a parameter that, in the recursion, gets the original value + 1).
Usually, though, recursively called named templates are seldom the best option. Can you tell use a bit more in detail what you'd like to achieve with your FOR loop?
Kind regards,
HFS
It might be easier to count the amount of times you enter your for-each by writing an extensive XPath.
I would go for
<xsl:value-of select="count( x-path to count nodes)"/>
So if you'd like to know the amount of times you enter a certain for-each, you use the same xpath to count the amount of nodes.
Why not simply use the count function?
more pseudo code:
<value-of select="count(child::*[where clause = ""])"/>
2 examples of where I use the count function to find the number of children that fit a certain set of requirements:
Hi Happyfanaticsalsero,
I have an xslt that looks for child nodes of type X in a tree, I want it to count each child node and then show the value but, I can't store the count of each node in the variable.Here is an example:
Tree:
Parent
- Parent1
- Child1
- Child2
- Parent2
- Child1
- Child 2
What code should do:
Count child nodes and show the number of child nodes. ( In this case the value shoud be 4 )
Status:
The function shows the childs perfectly but, as I said before, I need to store the nodes number in a variable to show it later.
Thanks beforehand.
Regards,
Eduardo Macho
Hi Rik,
Thanks for your answer, I am gonna test it right now.
Regards,
Eduardo Macho
Hi again,
Imagine I want to show in screen a fixed number of nodes, I should use a counter to know when to stop showing nodes.Right?
Thanks beforehand.
Regards,
Eduardo Macho
perhaps you're looking for this?
Pagina with XSLT:
http://www.nibble.be/?p=11
Happyfanaticsalsero,
Thanks for your answer but, that is not exactly what I am looking for.
I want to show a fixed number of nodes, that's why I want to know how many nodes I have shown.
I think I can store all childs information within a variable and then show only the number of nodes that I want. Is this correct?
Regards,
Eduardo Macho
Eduardo: you can use the position() function instead of a self-made counter
Hi folks,
I did it!. Thank you for all your answers.
Sincere regards,
Eduardo Macho
Rik,
I used position(), now it is working fine.
Regards,
Eduardo Macho
is working on a reply...