Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 08, 2012 @ 20:55
    Simon Dingley
    0

    Count nodes Between a previous sibling and current node

    I've been fighting with this for a while now and I think I can no longer spot the obvious, hopefully someone else can!?

    Here is my current XPath which returns the same result for each node I attempt it on and so therefore is wrong somehow:

    count(preceding-sibling::EventSessionFolder[Exslt.ExsltStrings:lowercase(sessionType) = 'break'][1]/following-sibling::EventSessionFolder [@id != current()/@id])

    What this should do is walk back up to the first EventSessionFolder node with a sessionType of 'break'. It should then count all EventSessionFolders between the Break and the Current Session Folder. It should not include the current session folder in the count. 

    Here is some sample XML:

    <Event>
        <EventSessionFolder>
            <sessionType>Session type 1</sessionType>
        </EventSessionFolder>
        <EventSessionFolder>
            <sessionType>Session type 2</sessionType>
        </EventSessionFolder>
        <EventSessionFolder>
            <sessionType>Break</sessionType>
        </EventSessionFolder>
        <EventSessionFolder>
            <sessionType>Session type 3</sessionType>
        </EventSessionFolder>
        <EventSessionFolder>
            <sessionType>Session type 4</sessionType>
        </EventSessionFolder>
        <EventSessionFolder>
            <sessionType>Session type 5</sessionType>
        </EventSessionFolder>
    </Event>

    Expected result:

    If I was currently in the EventSessionFolder in bold above, the corect count should be two i.e 2 EventSessionFolders after the break and before the current EventSessionFolder.

    I hope someone can enlighten me as to what I am missing here?

    Simon

     

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 08, 2012 @ 23:19
    Lee Kelleher
    0

    Hi Simon,

    Looking over the logic... is the example XML missing "id" attributes from the "EventSessionFolder" nodes?  (curious if these are Umbraco nodes, or general XML?)

    Initially your XPath looks correct, but would need to test/play with it.

    Cheers, Lee.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 08, 2012 @ 23:47
    Simon Dingley
    0

    Hang fire - I think I found the issue elsewhere in my XSLT. Coming back to it with a fresh pair of eyes in the morning.

    It is Umbraco XML, I just hand crafted the example above so the standard attributes are missing in the example, sorry.

    Thanks for validating my logic - good to know that it looked correct :)

    Cheers, Si

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jul 13, 2012 @ 00:19
    Chriztian Steinmeier
    0

    Hi Simon,

    Just saw this and wanted to chip in (don't know if you managed to solve it already).

    The problem with the XPath you're using is that it only works as long as your 'current' is the last one - that may always be the case, but the day it isn't, you'll probably begin tearing out that precious hair of yours :-)

    Here's an alternate calculation that makes sure to only use the nodes between the 'Break' and 'current':

    <!-- Grab the equivalent of position() for the Break node -->
    <xsl:variable name="breakpos" select="1 + count(preceding-sibling::EventSessionFolder[sessionType = 'Break'][1]/preceding-sibling::EventSessionFolder)" />
    <!-- Count the nodes between -->
    <xsl:value-of select="count(preceding-sibling::EventSessionFolder[position() &gt; $breakpos])" />
    

    /Chriztian

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 15, 2012 @ 21:54
    Simon Dingley
    0

    Hi Chriztian,

    Because of time restrictions I had to cheat a little by using javascript to assist in achieving the end goal however it was bugging me that I could get so close and yet still not solve this so thanks for the contribution and I will be sure to see if it solves my problem and allows me to remove my javascript *hack*.

    Thanks again.

    Simon

Please Sign in or register to post replies

Write your reply to:

Draft