Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 11:07
    Eduardo
    0

    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

  • Happyfanaticsalsero 59 posts 81 karma points
    Oct 19, 2010 @ 11:11
    Happyfanaticsalsero
    0

    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

  • Happyfanaticsalsero 59 posts 81 karma points
    Oct 19, 2010 @ 11:12
    Happyfanaticsalsero
    0

    It might be easier to count the amount of times you enter your for-each by writing an extensive XPath.

  • Happyfanaticsalsero 59 posts 81 karma points
    Oct 19, 2010 @ 11:15
    Happyfanaticsalsero
    0

    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.

  • Rik Helsen 670 posts 873 karma points
    Oct 19, 2010 @ 11:16
    Rik Helsen
    0

    Why not simply use the count function?

     

    more pseudo code:

    <value-of select="count(child::*[where clause = ""])"/>

  • Rik Helsen 670 posts 873 karma points
    Oct 19, 2010 @ 11:19
    Rik Helsen
    0

    2 examples of where I use the count function to find the number of children that fit a certain set of requirements:

     

    <xsl:if test="count(current()/child::* [@isDoc]) &gt; 0 ">
           
    <xsl:if test="count(umbraco.library:GetXmlNodeById(1127)/* [@isDoc and relatedAirport= current()/@id ])">
               

     
  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 11:21
    Eduardo
    0

    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

  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 11:23
    Eduardo
    0

    Hi Rik,

    Thanks for your answer, I am gonna test it right now.

    Regards,
    Eduardo Macho

  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 11:26
    Eduardo
    0

    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

  • Happyfanaticsalsero 59 posts 81 karma points
    Oct 19, 2010 @ 11:28
    Happyfanaticsalsero
    0

    perhaps you're looking for this?

    Pagina with XSLT:

    http://www.nibble.be/?p=11

  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 11:35
    Eduardo
    0

    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

  • Rik Helsen 670 posts 873 karma points
    Oct 19, 2010 @ 11:50
    Rik Helsen
    0

    Eduardo: you can use the position() function instead of a self-made counter

     

  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 11:50
    Eduardo
    0

    Hi folks,

    I did it!. Thank you for all your answers.

    Sincere regards,
    Eduardo Macho

  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 12:07
    Eduardo
    0

    Rik,

    I used position(), now it is working fine.

    Regards,
    Eduardo Macho

  • 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.

Please Sign in or register to post replies