Copied to clipboard

Flag this post as spam?

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


  • Thomas Kahn 602 posts 506 karma points
    Oct 24, 2011 @ 17:24
    Thomas Kahn
    0

    Recursive property in XSLT when the property contains CDATA (property never empty)?

    Hi!

    I'm working on a solution where a page that does not have a user set value for a particular property should continue looking up the node tree until it finds a page where the property is set.

    I found this example:

    <xsl:variable name="imageSource" select="$currentPage/ancestor-or-self::* [string(bannerImage)!=''] [1]"></xsl:variable>

    The problem in my case is that the property "bannerImage" is never empty. Instead it looks like this:

    <bannerImage><![CDATA[]]></bannerImage>

    The result is that the XSLT above doesn't work as it's supposed to - it never continues looking up the node tree since the string is never empty.

    How do I handle this?

    Regards,
    Thomas

  • Thomas Kahn 602 posts 506 karma points
    Oct 24, 2011 @ 17:47
    Thomas Kahn
    0

    Or?

    According to this answer on Stackoverflow, an empty CDATA is equal to an empty tag?
    Is this still true for umbraco? If so, what is wrong with my code?

    The property is present on all pages on the site since it's part of my master doctype. So the script will always find the property. But only a few pages have a value set for this property. If a page doesn't have a value set it should look at it's parent. And if the parent doesn't have it either, it should look at its parent and so on all the way up to the homepage.

    /Thomas

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 24, 2011 @ 18:06
    Chriztian Steinmeier
    3

    Hi Thomas,

    A CDATA Section is nothing but a (very) special way of marking up something that's not to be parsed as anything else but text - so an "empty" CDATA Section is the same as an empty element (tag).

    The best way to test for that is using the normalize-space() function... meanwhile, your variable ends up selecting the document node, not the bannerImage property, so here's the XPath needed to "fix" it:

    <xsl:variable name="imageSource" select="$currentPage/ancestor-or-self::*[normalize-space(bannerImage)][1]/bannerImage" />

    /Chriztian

  • Thomas Kahn 602 posts 506 karma points
    Oct 25, 2011 @ 09:14
    Thomas Kahn
    0

    Hi Chriztian!

    I realize that my example was not complete - I did have a continued path to the actual property. But the normalize-space function was what made the difference!

    Thanks! 

    /Thomas

Please Sign in or register to post replies

Write your reply to:

Draft