Copied to clipboard

Flag this post as spam?

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


  • Jon Ratcliffe 20 posts 47 karma points
    Jul 28, 2011 @ 13:32
    Jon Ratcliffe
    0

    Recursively search up the tree for a property (problem)

    Hi All,

    I'm having a problem with an xslt macro that I have written. All my document types have a cssColourway dropdown menu that can optionally be set to one of about 10 colours. If the colour is set then the correct css class gets rendered into a class="" attribute in the html - this all works fine.

    To avoid the client having to set this on every single page, when a colour has not been set, I would like to look up the tree to parent nodes until I find a node that has a colour and use this. As a real world example - the client could set the colour on a section landing page and all the child pages would then use this colour unless explicitly overriden.

    A cut down version of the xml would look something like this:

    <Homepage id="1101">
    <cssColourway><![CDATA[Purple]]></cssColourway>
    <Section id="1102">
    <cssColourway><![CDATA[Red]]></cssColourway>
    <ContentPage id="1103">
    // inner xml
    </ContentPage>
    </Section>
    </Homepage>

    Assuming that $currentPage is id="1103", I am trying to get it to find the colour Red from its parent (please bear in mind that this isn't always neccessarily going to be it's immediate parent but needs to be the first parent with a non empty cssColourway element).

    I am using this XSLT to try and get the first parent/ancestor that has a cssColourway element that is not empty:

    <xsl:value-of select="$currentPage/ancestor-or-self::* [cssColourway != '']/cssColourway"/>

    This seems to return Purple (node id 1101) every time.

    Does anyone know why this XSLT query is ignoring the first parent with a non empty cssColourway element and continuing to the furthest grandparent?

     

  • Jon Ratcliffe 20 posts 47 karma points
    Jul 28, 2011 @ 13:43
    Jon Ratcliffe
    0

    Ha!

    Fixed my own problem about 2 minutes after posting this.

    For anyone interested, I need to select only the first element being returned as by default I am presuming I was getting the last matching node.

    So this:

    <xsl:value-ofselect="$currentPage/ancestor-or-self::* [cssColourway != '']/cssColourway"/>

    becomes this:

    <xsl:value-ofselect="$currentPage/ancestor-or-self::* [cssColourway != ''][1]/cssColourway"/>

     

     

    Jon.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jul 28, 2011 @ 23:42
    Chriztian Steinmeier
    0

    Hi Jon,

    Just to explain what's going on to future "googlers": Whenever you select something with XPath, you get a "node-set" back, so in your case, all the cssColourway properties of all the ancestors having a non-empty value for that property.

    The value-of instruction is expected to return a string value, it will take the this from the first node in the set - at this point, the order of nodes in the set will be in "document order", so you get the value of the topmost node.

    Here's the (somewhat) confusing part:

    The preceding:: and ancestor:: axes (and their -or-self:: counterparts) works in *reverse* document order, so adding an index-predicate (the [1]-thing) to the expression will take the first preceding node in that set, but *not* the first in document order.

    /Chriztian

      

Please Sign in or register to post replies

Write your reply to:

Draft