Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    Oct 13, 2011 @ 14:00
    Gordon Saxby
    0

    Trying to Group By with XSLT

    Based on other posts and Internet searching, I have ended up with the following attempt to group the output of a xsl:for-each loop.

    Sort the nodes based on the template used:

          <xsl:sort select="@template" order="ascending"/>


    Then detect if the template changes

          <xsl:if test="./@template[not(. = preceding-sibling/@template)]">

     

    Unfortunately, the xsl:if is not working ... it always thinks the value of @template is different.

    Where have I gone / what have I done wrong?

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 13, 2011 @ 14:19
    Chriztian Steinmeier
    1

    Hi Gordon,

    The test fails because it's actually asking for the <preceding-sibling> childnode's template attribute, which, of course, doesn't exist.

    Try this:

    <xsl:if test="not(preceding-sibling::*[1]/@template = current()/@template)">

    - that should get some better results...

    /Chriztian

  • Rodion Novoselov 694 posts 859 karma points
    Oct 13, 2011 @ 14:23
    Rodion Novoselov
    0

    Hi. Try to replace 'preceding-sibling' with 'preceding-sibling::*[1]'.

     

  • Gordon Saxby 1461 posts 1883 karma points
    Oct 13, 2011 @ 14:30
    Gordon Saxby
    0

    Thanks all, this is what I have now and seems to work ...

    <xsl:if test="position() = 1 or (./@template != ./preceding-sibling::* [@isDoc][1]/@template)">

    I needed the position() = 1 because otherwise the first template name failed to output.

  • Gordon Saxby 1461 posts 1883 karma points
    Oct 13, 2011 @ 14:43
    Gordon Saxby
    0

    Ah, spoke too soon! I assumed that the "preceding-sibling" would be based on the sort order, however it appears to be based on the original order of the nodes in the database / CMS.

    The actual xsl:sort IS working.

    How do I make it check the preceding sibling in the search results?!

  • Gordon Saxby 1461 posts 1883 karma points
    Oct 13, 2011 @ 17:02
    Gordon Saxby
    0

    In the end, I took a different approach! I first did a for-next loop of the category nodes that I wanted to group by, then I did an inner loop which searched for each type.

    It seems to work so I'll go with it ;-)

Please Sign in or register to post replies

Write your reply to:

Draft